replace()

Search for a string inside a main string and if found then replace it with another string
main_string.replace(search_string, replace_string, count)
search_string: Required , string to search within main string
replace_string: Required, string to replace if matching is found
count: ( Optional ) Number of times the replacement to happen. By default all matches will be replaced.

The original string is not changed.
If the search string is not found inside main string, then main string is returned as output.
my_str='Welcome to PHP section of  plus2net'
output=my_str.replace('PHP','Python')
print(output)
Ouptut
Welcome to Python section of  plus2net
Replacing all occurrences of search string
my_str='Welcome to PHP section of  plus2net. PHP has many buit-in functions'
output=my_str.replace('PHP','Python')
print(output)
Output
Welcome to Python section of  plus2net. Python has many buit-in functions
Using optional parameter count, we can replace only one occurrence.
my_str='Welcome to PHP section of  plus2net. PHP has many buit-in functions'
output=my_str.replace('PHP','Python',1)
print(output)
Output
Welcome to Python section of  plus2net. PHP has many buit-in functions
No matching is found, so input string is returned.
my_str='Welcome to PHP section of  plus2net'
output=my_str.replace('MySQL','Python')
print(output) # Nothing replaced as no matching found
Output is here
Welcome to PHP section of  plus2net
All String methods



plus2net.com



Post your comments , suggestion , error , requirements etc here




We use cookies to improve your browsing experience. . Learn more
HTML MySQL PHP JavaScript ASP Photoshop Articles FORUM . Contact us
©2000-2020 plus2net.com All rights reserved worldwide Privacy Policy Disclaimer