Advertisement
# Signature of replace() method
replace(oldvalue, newvalue, count)
Parameters | Description |
---|---|
oldvalue | oldvalue is a value which will be replaced by a newvalue. A non-optional attribute. |
newvalue | newvalue is a value which will replace the value specified by oldvalue. A non-optional attribute. |
count | count is the number of occurrences of oldvalue to replace with the newvalue, by default all the occurrences of oldvalue are replaced by the newvalue. An optional attribute. |
# Python - Program to replace a particular value with another value in a string using replace() method.
# Creating a string
s = 'The Power of Python'
print('Original String is : ', s)
# Calling the replace() method to replace P with X in the invoked string.
print('String\'s value after replacing P with X : ', s.replace('P', 'X'))
Original String is : The Power of Python
String's value after replacing P with X : The Xower of Xython
# Python - Program to replace a particular value with another value in a string using replace() method.
# Creating a string
s = 'Never give up!'
# Priting the original string
print('Original String is : ', s)
# Calling the replace() method to replace e with E in the invoked string.
print('String\'s value after replacing e with E : ', s.replace('e', 'E'))
# Checking the original value of the string after calling replace () on it
print('String\'s value after replace() is called : ', s)
Original String is : Never give up!
String's value after replacing e with E : NEvEr givE up!
String's value after replace() is called : Never give up!
# Python - Program to replace a particular value with another value in a string using replace() method.
# Creating a string
s = 'Being around nature is an amazing feeling!'
# Priting the original string
print('Original String is : ', s)
# Calling the replace() method to replace e with E in the invoked string.
# Reference to the old string is now pointing to the new string returned by calling the replace()
# Old string is lost and not referenced by any reference variable anymore.
s = s.replace('e', 'E')
# Printing the new value referenced by the reference variable
print('String\'s value after replace() is called : ', s)
Original String is : Being around nature is an amazing feeling!
String's value after replace() is called : BEing around naturE is an amazing fEEling!
Advertisement
# Python - Program to replace a particular value with another value in a string using replace() method.
# Creating a string
s = 'The Power of Python'
print('Original String is : ', s)
# Calling the replace() method to replace 1 occurrence of P with p in the invoked string.
print('String\'s value after replacing 1 occurrence of P with p : ', s.replace('P', 'p', 1))
# Calling the replace() method to replace 2 occurrence of o with O in the invoked string.
print('String\'s value after replacing 1 occurrences of o with O : ', s.replace('o', 'O', 2))
# Calling the replace() method to replace 1 occurrence of e with E in the invoked string.
print('String\'s value after replacing 1 occurrence of e with E : ', s.replace('e', 'E', 1))
Original String is : The Power of Python
String's value after replacing 1 occurrence of P with p : The power of Python
String's value after replacing 1 occurrences of o with O : The POwer Of Python
String's value after replacing 1 occurrence of e with E : ThE Power of Python
Advertisement
Advertisement
Please check our latest addition
C#, PYTHON and DJANGO
Advertisement