Python String replace method with Example – Python Tutorial
4 weeks ago codedestine 0
In this tutorial, we will learn about python string replace method.
replace method :
This method returns a copy of the string in which all the occurrences of old string ( passed as a first argument ) had been replaced by the new string ( passed as a second argument ). If optional third argument count is passed, then replace method limits the replacement operation ( new string replacing old string ) to the maximum of number count.
Syntax : < String Object >.join( old_String, new_String[, count] ) : < String Object >
# create one string and assign it to variable s s = 'I am here and only here and only here' # call replace() method on string s by passing 'here' (old string) and 'there' (new string) as an argument output = s.replace('here','there') # print the value of variable output print(output) # call replace() method on string s by passing 'here' (old string), 'there' (new string) and 2 as an argument output = s.replace('here','there',2) # print the value of variable output print(output)
That’s all for Python String replace method. If you liked it, please share your thoughts in comments section and share it with others too.