replace()

All String methods


Youtube Live session on Tkinter

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.
This is case sensitive string search.
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 built-in functions
Using optional parameter count, we can replace only one occurrence.
my_str='Welcome to PHP section of  plus2net. PHP has many built-in functions'
output=my_str.replace('PHP','Python',1)
print(output)
Output
Welcome to Python section of  plus2net. PHP has many built-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


Subscribe to our YouTube Channel here


Subscribe

* indicates required
Subscribe to plus2net

    plus2net.com



    Post your comments , suggestion , error , requirements etc here





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