I used regular expressions to get a string from a web page and part of the string may contain something I would like to replace with something else. How would it be possible to do this? My code is this, for example:
stuff = "Big and small" if stuff.find(" and ") == -1: # make stuff "Big/small" else: stuff = stuff
>>> help(str. replace) Help on method_descriptor: replace(...) S. replace (old, new[, count]) -> string Return a copy of string S with all occurrences of substring old replaced by new. If the optional argument count is given, only the first count occurrences are replaced.
replace() is an inbuilt function in the Python programming language that returns a copy of the string where all occurrences of a substring are replaced with another substring. Parameters : old – old substring you want to replace. new – new substring which would replace the old substring.
replace() method helps to replace the occurrence of the given old character with the new character or substring. The method contains the parameters like old(a character that you wish to replace), new(a new character you would like to replace with), and count(a number of times you want to replace the character).
The Java string replace() method will replace a character or substring with another character or string. The syntax for the replace() method is string_name. replace(old_string, new_string) with old_string being the substring you'd like to replace and new_string being the substring that will take its place.
>>> stuff = "Big and small" >>> stuff.replace(" and ","/") 'Big/small'
Use the replace()
method on string:
>>> stuff = "Big and small" >>> stuff.replace( " and ", "/" ) 'Big/small'
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With