Tuesday, 30 May 2023

Replace all the occurrences of sub string in the given string

Using string.replace() method, we can replace all the matching substrings in a string.

 

replace_sub_string.py

str1 = 'hello there, hi there'

str2 = str1.replace('there', 'Krishna')

print('str1 : ', str1)
print('str2 : ', str2)

 Above snippet generate below output.

str1 :  hello there, hi there
str2 :  hello Krishna, hi Krishna

 


  

Previous                                                 Next                                                 Home

No comments:

Post a Comment