How to use string.replace() in python 3.x
PythonPython 3.xStringReplacePython Problem Overview
The string.replace()
is deprecated on python 3.x. What is the new way of doing this?
Python Solutions
Solution 1 - Python
Solution 2 - Python
replace()
is a method of <class 'str'>
in python3:
>>> 'hello, world'.replace(',', ':')
'hello: world'
Solution 3 - Python
The replace() method in python 3 is used simply by:
a = "This is the island of istanbul"
print (a.replace("is" , "was" , 3))
#3 is the maximum replacement that can be done in the string#
>>> Thwas was the wasland of istanbul
# Last substring 'is' in istanbul is not replaced by was because maximum of 3 has already been reached
Solution 4 - Python
You can use str.replace() as a chain of str.replace(). Think you have a string like 'Testing PRI/Sec (#434242332;PP:432:133423846,335)'
and you want to replace all the '#',':',';','/'
sign with '-'
. You can replace it either this way(normal way),
>>> string = 'Testing PRI/Sec (#434242332;PP:432:133423846,335)'
>>> string = string.replace('#', '-')
>>> string = string.replace(':', '-')
>>> string = string.replace(';', '-')
>>> string = string.replace('/', '-')
>>> string
'Testing PRI-Sec (-434242332-PP-432-133423846,335)'
or this way(chain of str.replace())
>>> string = 'Testing PRI/Sec (#434242332;PP:432:133423846,335)'.replace('#', '-').replace(':', '-').replace(';', '-').replace('/', '-')
>>> string
'Testing PRI-Sec (-434242332-PP-432-133423846,335)'
Solution 5 - Python
Try this:
mystring = "This Is A String"
print(mystring.replace("String","Text"))
Solution 6 - Python
str.replace
of Python 3
Official doc for official doc: Python 3's str.replace
> str.replace(old, new[, count]) > > Return a copy of the string with all occurrences of substring old replaced by new. If the optional argument count is given, only the first count occurrences are replaced.
corresponding VSCode
's syntax notice is:
> str.replace(self: str, old, new, count) -> str
str.replace
Two method to use - Method 1: use builtin str's replace ->
str.replace(strVariable, old, new[, count])
replacedStr1 = str.replace(originStr, "from", "to")
- Method 2: use str variable's replace ->
strVariable.replace(old, new[, count])
replacedStr2 = originStr.replace("from", "to")
Full demo
code:
originStr = "Hello world"
# Use case 1: use builtin str's replace -> str.replace(strVariable, old, new[, count])
replacedStr1 = str.replace(originStr, "world", "Crifan Li")
print("case 1: %s -> %s" % (originStr, replacedStr1))
# Use case 2: use str variable's replace -> strVariable.replace(old, new[, count])
replacedStr2 = originStr.replace("world", "Crifan Li")
print("case 2: %s -> %s" % (originStr, replacedStr2))
output:
case 1: Hello world -> Hello Crifan Li
case 2: Hello world -> Hello Crifan Li
screenshot:
My related (Chinese) post: 【详解】Python 3中字符串的替换str.replace
Solution 7 - Python
FYI, when appending some characters to an arbitrary, position-fixed word inside the string (e.g. changing an adjective to an adverb by adding the suffix -ly), you can put the suffix at the end of the line for readability. To do this, use split()
inside replace()
:
s="The dog is large small"
ss=s.replace(s.split()[3],s.split()[3]+'ly')
ss
'The dog is largely small'
Solution 8 - Python
Simple Replace: .replace(old, new, count) .
text = "Apples taste Good."
print(text.replace('Apples', 'Bananas')) # use .replace() on a variable
Bananas taste Good. <---- Output
print("Have a Bad Day!".replace("Bad","Good")) # Use .replace() on a string
Have a Good Day! <----- Output
print("Mom is happy!".replace("Mom","Dad").replace("happy","angry")) #Use many times
Dad is angry! <----- Output
Solution 9 - Python
ss = s.replace(s.split()[1], +s.split()[1] + 'gy')
# should have no plus after the comma --i.e.,
ss = s.replace(s.split()[1], s.split()[1] + 'gy')