Roopa Shekhawat
Roopa Shekhawat

Reputation: 35

Trying to use replace function (python)

I am learning python ,i.e., version 3.8 While trying to replace a string substring using replace() function.

str = "Python"
str.replace("th", "t")
print(str)

Output: Python

Expected Output: Pyton

Upvotes: 0

Views: 93

Answers (1)

Ray
Ray

Reputation: 1676

replace() returns a copy of the string with all occurrences of substring old replaced by new

You need to assign to a variable to store the copy

str = "Python"
replaced_str= str.replace("th", "t")
print(replaced_str)

enter image description here

Upvotes: 1

Related Questions