Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR PYTHON

search and replace in python

# replace in Python, works like search and replace in word processor
greet = 'Hello Bob'
nstr1 = greet.replace('Bob', 'Jane')
print(nstr1)         # Output: Hello Jane
# Initial string doesn't change
print(greet)
# replaces all occurrences of the sub string
nstr2 = greet.replace('o', 'X')
print(nstr2)        # Output: HellX BXb
# We can do replace only for number of times
nstr3 = greet.replace('o', 'O', 1)
print(nstr3)        # Output: HellO Bob
Source by www.py4e.com #
 
PREVIOUS NEXT
Tagged: #search #replace #python
ADD COMMENT
Topic
Name
6+4 =