Q:

python replace all occurrences in string


# Python3 program to demonstrate the  
# use of replace() method   
  
string = "geeks for geeks geeks geeks geeks" 
   
# Prints the string by replacing geeks by Geeks  
print(string.replace("geeks", "Geeks"))  
  
# Prints the string by replacing only 3 occurrence of Geeks   
print(string.replace("geeks", "GeeksforGeeks", 3)) 
14
>>> 'no one knows how'.replace('no', 'yes')
'yes one kyesws how'
1
# Python3 program to demonstrate the  
# use of replace() method   
  
string = "geeks for geeks geeks geeks geeks" 
   
# Prints the string by replacing geeks by Geeks  
print(string.replace("geeks", "Geeks"))  
  
# Prints the string by replacing only 3 occurrence of Geeks   
print(string.replace("geeks", "GeeksforGeeks", 3)) 
4
#our text
text='Hello! there/ how may. I help, you' #yes, it seems weird
     #replacing ("THIS" with "THIS") 
replaced_text=text.replace("!", "?")
print(replaced_text)
#output
#Hello? there/ how may. I help, you
0

New to Communities?

Join the community