Strings
Strings
Solution
Q2 Give the elements of the following string which are present at the given index numbers:
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 '
Identify which of the following operations is possible on strings. If possible, write Python
statements for the following:
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:
1. str2=str1+str2
2. str1=str2+str1
3. str2=str*2
4. "work" in str1
5. "off" not in str2
Solution
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']
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.
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
>>> print(str5.isupper())
False
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.
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('@')
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
Example
I am Happy".title()
I Am Happy
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)
Q2. Write Python script to input a string and display it in reverse order.
Solution
str=input("Enter a string")
rev=str[::-1]
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(" ")
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
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("Numbers: ", z)
n = len(s)
m=""
m = m +s[i].upper()
m = m +s[i-1]
elif (s[i].isupper()):
m = m + s[i].lower()
else:
m = m +'&'
print(m)