How to use string.replace() in python 3.x

The Question :

307 people think this question is useful

The string.replace() is deprecated on python 3.x. What is the new way of doing this?

The Question Comments :
  • FWIW, I had the same confusion. Google “python string replace” took me to old deprecated string functions in python 2.7. IMHO, that section could use a big bold box explaining “string.xxx()” vs “xxx(string)”, and directing people to the non-deprecated string methods, e.g. to docs.python.org/library/stdtypes.html#string-methods
  • Python documentation is an absolute shambles considering it is touted as an ideal first language. Quite often the stuff is there, but because of the poor way its organized, its often not indexed well by search engines or even their own site. Look at ToolMakerSteve’s link, core string functions are lumpted in to standard types. This does not come up when you search for string functions.
  • To be clear: string.replace() is actually not deprecated on Python 3.
  • It’s in the built-in types for 3.x docs.python.org/3.7/library/stdtypes.html#str.replace
  • Sometimes people write “str.replace” when they mean [your string variable].replace. Because ‘str’ is also the name of the relevant class, this can be confusing.

The Answer 1

339 people think this answer is useful

As in 2.x, use str.replace().

Example:

>>> 'Hello world'.replace('world', 'Guido')
'Hello Guido'

The Answer 2

115 people think this answer is useful

replace() is a method of <class 'str'> in python3:

>>> 'hello, world'.replace(',', ':')
'hello: world'

The Answer 3

16 people think this answer is useful

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

The Answer 4

8 people think this answer is useful

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)'

The Answer 5

3 people think this answer is useful

Try this:

mystring = "This Is A String"
print(mystring.replace("String","Text"))

The Answer 6

1 people think this answer is useful

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'

The Answer 7

0 people think this answer is useful
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')

The Answer 8

0 people think this answer is useful

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

Add a Comment