PYTHON STRINGS
Q1 Give the output:
1. >>> print('''Rajan said, "It’ a lovely day"''')
2. >>> print("Ram said, \"I AM LEARNING PYTHON")
3. >>> print("Ram said, "I AM LEARNING PYTHON"")
4. >>> print ('Sangeeta said, "I’ve got good grades" ')
5. >>> print ('Sangeeta said, "I\’ve got good grades" ')
6. >>> print("A goal \n without a plan is just a \t wish")
Solution
1. Rajan said, "It’s a lovely day"
2. Ram said, "I AM LEARNING PYTHON
3. SyntaxError: invalid syntax
4. Sangeeta said, "I’ve got good grades"
5. A goal
without a plan is just a wish
Q2 Give the elements of the following string which are present at the given index numbers:
str="Hard work pays off"
1. str [2]
2. str [-3]
3. str [2:5]
4. str [2:5:2]
5. str [:5]
6. str [3:]
7. str [::2]
8. str [::-2]
9. str [-5:-2]
10. str [5:2:-1]
11. str [-2:-5:-1]
12. str [-2:-5:-2]
Solution
1. 'r'
2. 'o'
3. 'rd '
4. 'r '
5. 'Hard '
6. 'd work pays off'
7. 'Hr okpy f'
8. 'fosa rwda'
9. 's o'
10. 'w d'
11. 'fo '
12. 'f '
Q3. Consider the following strings:
str1="Dreams don’t work unless you do"
str2="Hard work pays off"
Identify which of the following operations is possible on strings. If possible, write Python
statements for the following:
1. To add “Thanks” at the end of the string str1.
2. To add string str2 at the end of the string str1.
3. To replace the word “unless” in the string str1 with “if”.
4. To insert the word “Thanks” after the word “Dreams” in string str1.
S = str1[0:7] + ‘Thanks ‘+str1[7:]
5. To remove the third character from the string str1.
6. To delete the entire string str1.
7. To traverse the string str1 using a for loop.
8. To traverse the string str2 using a while loop.
Solution
a. print(str1+"Thanks")
b. print(str1+str2)
c. print(str1.replace("unless", "if"))
d. Insertion in between is not possible in the strings
e. Deletion of a character is not possible in the strings
f. del str1
g. for I in str1:
print(I)
h. i=0
while i<len(str2):
print(str[i])
i=i+1
Q4. Consider the strings str1 and str2 and give the output of the following Python statements:
str1="Dreams don’t work unless you do"
str2="Hard work pays off"
1. str2=str1+str2
2. str1=str2+str1
3. str2=str*2
4. "work" in str1
5. "off" not in str2
Solution
1. Dreams don’t work unless you doHard work pays off
2. Hard work pays offDreams don’t work unless you do
3. Dreams don’t work unless you doDreams don’t work unless you do
4. True
5. False
Q5. Give the output of the following Python statements:
str1="Information age #2018"
str="@"
1. len(str1)
2. print(str1.capitalize())
3. print(str1.isalnum())
4. print(str1.isalpha())
5. print(str1.isdigit())
6. print(str1.lower())
7. print(str1.upper())
8. print(str1.islower())
9. find('In')
10. find('a')
11. find('a', 8)
12. find('in')
13. print (str1.isspace())
14. print(str1.isupper())
15. istitle()
16. join(str1)
17. print(str1.replace('a', '@'))
18. partition('age')
19. split('e')
Solution
1. 21
2. Information age #2018
3. False
4. False
5. False
6. information age #2018
7. INFORMATION AGE #2018
8. False
9. 0
10. 6
11. 12
12. -1
13. False
14. False
15. False
16. 'I@n@f@o@r@m@a@t@i@o@n@ @a@g@e@ @#@2@0@1@8'
17. Inform@tion @ge #2018
18. ('Information ', 'age', ' #2018')
19. ['Information ag', ' #2018']
Q6. What is the difference between indexing and slicing?
Solution
Indexing is used to retrieve an element from an ordered data type like string, list, tuple etc.
Slicing is used to retrieve a subset of values from an ordered data type. A slice of a string is
basically its sub-string.
Q7.Give the difference between upper() and isupper() functions of string.
Solution
The upper() function returns the copy of the string with all the letters in uppercase. The isupper()
function returns True if the string is in uppercase.
Example
>>> print(str3.upper())
ERATECH
>>> str5='tech era *2018*'
>>> print(str5.isupper())
False
Q8.Give the difference between index() and find() methods of string.
Solution
Both index() and find() methods are used to find the occurance of specified value in a given
string. The only difference is that the find() method returns -1 if the value is not found and
index() method will raise an exception.
Q9.Give the difference between partition() and split() methods of string.
Solution
string.split() - It splits the whole string on all occurrences of the given argument
Example
"cat@bat@mat".split("@")
('cat','bat','mat')
string.partition() - Splits the string in three parts on the first occurrence of the given argument
i.e. before the argument, the argument itself and the part after the argument
Example
"cat@bat@mat".partition('@')
('cat', '@', ' bat@mat ')
Q10.Give the difference between capitalize() and title() methods of string.
Solution
string.capitalize() - This function capitalizes the first letter of the string keeping all other letters
in lowercase.
Example
"I am Happy".capitalize()
I am happy
string.title() - Capitalizes the first character of each word in a string
Example
I am Happy".title()
I Am Happy
Practical / Programming questions
Q1. Write Python script to input a string and a character and count the number of
occurrences of the character in the string.
Solution
str=input("Enter a string")
chr=input("Enter a character")
c=str.count(chr)
print("Total number of occurrences are:", c)
Q2. Write Python script to input a string and display it in reverse order.
Solution
str=input("Enter a string")
rev=str[::-1]
print("Reversed string is:", rev)
Q3. Write Python script to input a string and check whether it is a palindrome or not.
Solution
str=input("Enter a string")
rev=str[::-1]
if str==rev:
print("Palindrome")
else:
print("Not a palindrome")
Q4. Write Python script to input a string and count the number of words in it.
Solution
str=input("Enter a string")
c=str.count(" ")
print("No. of words are:",c+1)
Q5. Write Python script to input a string and count the number of words beginning with
'A' or 'a'.
Solution
str=input("Enter a string")
x=str.count(" A")
y=str.count(" a")
if str[0]=="a":
y=y+1
if str[0]=="A":
x=x+1
print("No. of words starting with A are:",x)
print("No. of words starting with a are:",y)
Q6. Write Python script to input a string and replace the first letter of every word to
uppercase and then display it.
Solution
str=input("Enter a string")
x=str.title()
print(x)
Q7. Write Python script to input a string and replace all occurrences of the word ‘the’
with ‘that’.
Solution
str=input("Enter a string")
x=str.replace("the","that")
print(x)
Q8. Write Python script to input a string and count and display the number of capital
alphabets, small alphabets and numbers.
Solution
str=input("Enter a string")
x,y,z=0,0,0
for i in str:
if i.isupper():
x=x+1
elif i.islower():
y=y+1
elif i.isdigit():
z=z+1
print("Upper case: ", x)
print("Lower case: ", y)
print("Numbers: ", z)
GIVE OUTPUT OF FOLLOWING CODE:
s="Hello India 2022!"
n = len(s)
m=""
for i in range(0, n):
if (s[i] >= 'a' and s[i] <= 'm'):
m = m +s[i].upper()
elif (s[i] >= 'n' and s[i] <= 'z'):
m = m +s[i-1]
elif (s[i].isupper()):
m = m + s[i].lower()
else:
m = m +'&'
print(m)