python / beginner
Snippet
String Replacement
The replace() method creates a new string by substituting a specific substring with a new one. Original strings remain unchanged.
snippet.py
1
2
3
text = "I like Java"new_text = text.replace("Java", "Python")print(new_text)
Breakdown
1
text = "I like Java"
Define a string containing the word to be replaced.
2
new_text = text.replace("Java", "Python")
Call the replace method to swap 'Java' for 'Python' in a new variable.
3
print(new_text)
Output the resulting updated string.