Computer Science
Consider the string str1 = "Global Warming".
Write statements in Python to implement the following:
(a) To display the last four characters.
(b) To replace all the occurrences of letter 'a' in the string with "*".
Python String Manipulation
4 Likes
Answer
(a)
str1 = "Global Warming"
last_four = str1[-4:]
print(last_four)
Output
ming
(b)
str1 = "Global Warming"
replaced_str = str1.replace('a', '*')
print(replaced_str)
Output
Glob*l W*rming
Answered By
2 Likes
Related Questions
Consider the following string mySubject:
mySubject = "Computer Science"What will be the output of the following string operations?
(i) print (mySubject [0:len(mySubject)])
(ii) print (mySubject[-7:-1])
(iii) print (mySubject[::2])
(iv) print (mySubject [len (mySubject) -1])
(v) print (2*mySubject)
(vi) print (mySubject[::-2])
(vii) print (mySubject[:3] + mySubject[3:])
(viii) print (mySubject.swapcase() )
(ix) print (mySubject.startswith('Comp') )
(x) print (mySubject.isalpha() )
Write the Python statement and the output for the following:
(a) Find the third occurrence of 'e' in 'sequence'.
(b) Change the case of each letter in string 'FuNcTioN'.
(c) Whether 'Z' exists in string 'School' or not.
What will be the output of the following code?
Text = "Mind@Work!" ln = len(Text) nText = "" for i in range(0, ln): if Text[i].isupper(): nText = nText + Text[i].lower() elif Text[i].isalpha(): nText = nText + Text[i].upper() else: nText = nText + 'A' print(nText)Input the string 'My School'. Write a script to partition the string at the occurrence of letter 'h'.