CBSE Class 11 Computer Science Question 13 of 19

Strings in Python — Question 10

Back to all questions
10
Question

Question 9

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 "*".

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