In the previous article, we have discussed Python String upper() Method with Examples
String replace() Method in Python:
The replace() method replaces a phrase specified with another phrase specified.
Syntax:
string.replace(oldvalue, newvalue, count)
Note: If nothing else is specified, all occurrences of the specified phrase will be replaced.
Parameters
oldvalue: This is Required. The string to look for.
newvalue: This is Required. The string will be used to replace the old value.
count: This is optional. A number indicating how many occurrences of the old value should be replaced. The default value is all occurrences.
Return Value:
The replace() method returns a copy of the string that replaces the old value with the new value. The original string remains unaltered.
If the old substring cannot be found, the copy of the original string is returned.
Examples:
Example1:
Input:
Given String = "hello this is hello btechgeeks hello " Given old value = "hello" Given new value = "hai"
Output:
The given string after replacing oldvalue{ hello } with new value{ hai } is: hai this is hai btechgeeks hai
Example2:
Input:
Given String = "welcome to to python to to programs" Given old value = "to" Given new value = "all" Given count = 3
Output:
The given string after replacing oldvalue{ to } with new value{ all } is: welcome all all python all to programs
String replace() Method with Examples in Python
Method #1: Using Built-in Functions (Static Input)
1)Without giving count Value
Approach:
- Give the string as static input and store it in a variable.
- Give the old value as static input and store it in another variable.
- Give the new value as static input and store it in another variable.
- Pass the given old value, new value as arguments to the replace() function for the given string to replace the old value with a new value in a given string.
- Store it in another variable.
- Print the above result i.e, The given string after replacing the old value with the given new value.
- The Exit of the program.
Below is the implementation:
# Give the string as static input and store it in a variable. gvn_fststr = "hello this is hello btechgeeks hello " # Give the old value as static input and store it in another variable. old_valu = "hello" # Give the new value as static input and store it in another variable. new_valu = "hai" # Pass the given old value, new value as arguments to the replace() function # for the given string to replace the old value with a new value # in a given string. # Store it in another variable. rslt = gvn_fststr.replace(old_valu, new_valu) # Print the above result i.e, The given string after replacing the old value # with the given new value. print("The given string after replacing oldvalue{", old_valu, "} with new value{", new_valu, "} is:") print(rslt)
Output:
The given string after replacing oldvalue{ hello } with new value{ hai } is: hai this is hai btechgeeks hai
2)With giving count Value
Approach:
- Give the string as static input and store it in a variable.
- Give the old value as static input and store it in another variable.
- Give the new value as static input and store it in another variable.
- Give the count as static input and store it in another variable.
- Pass the given old value, new value, count as arguments to the replace() function for the given string to replace the old value with a new value in a given string. Here count is a number indicating how many occurrences of the old value should be replaced.
- Store it in another variable.
- Print the above result i.e, The given string after replacing the old value with the given new value.
- The Exit of the program.
Below is the implementation:
# Give the string as static input and store it in a variable. gvn_fststr = "hello this is hello btechgeeks hello " # Give the old value as static input and store it in another variable. old_valu = "hello" # Give the new value as static input and store it in another variable. new_valu = "hai" # Give the count as static input and store it in another variable. gvn_cnt = 2 # Pass the given old value, new value, count as arguments to the replace() # function for the given string to replace the old value with a new value # in a given string. Here count is a number indicating how many occurrences # of the old value should be replaced. # Store it in another variable. rslt = gvn_fststr.replace(old_valu, new_valu, gvn_cnt) # Print the above result i.e, The given string after replacing the old value # with the given new value. print("The given string after replacing oldvalue{", old_valu, "} with new value{", new_valu, "} is:") print(rslt)
Output:
The given string after replacing oldvalue{ hello } with new value{ hai } is: hai this is hai btechgeeks hello
Method #2: Using Built-in Functions (User Input)
1)Without giving count Value
Approach:
- Give the string as user input using the input() function and store it in a variable.
- Give the old value as user input using the input() function and store it in another variable.
- Give the new value as user input using the input() function and store it in another variable.
- Pass the given old value, new value as arguments to the replace() function for the given string to replace the old value with a new value in a given string.
- Store it in another variable.
- Print the above result i.e, The given string after replacing the old value with the given new value.
- The Exit of the program.
Below is the implementation:
# Give the string as user input using the input() function and store it in a variable. gvn_str = input("Enter some Random String = ") # Give the old value as user input using the input() function and store it in another variable. old_valu = input("Enter some Random String(oldvalue) = ") # Give the new value as user input using the input() function and store it in another variable. new_valu = input("Enter some Random String(newvalue) = ") # Pass the given old value, new value as arguments to the replace() function # for the given string to replace the old value with a new value # in a given string. # Store it in another variable. rslt = gvn_str.replace(old_valu, new_valu) # Print the above result i.e, The given string after replacing the old value # with the given new value. print("The given string after replacing oldvalue{", old_valu, "} with new value{", new_valu, "} is:") print(rslt)
Output:
Enter some Random String = good morning btechgeeks good good morning Enter some Random String(oldvalue) = good Enter some Random String(newvalue) = bad The given string after replacing oldvalue{ good } with new value{ bad } is: bad morning btechgeeks bad bad morning
2)With giving count Value
Approach:
- Give the string as user input using the input() function and store it in a variable.
- Give the old value as user input using the input() function and store it in another variable.
- Give the new value as user input using the input() function and store it in another variable.
- Give the count as user input using the int(input()) function and store it in another variable.
- Pass the given old value, new value, count as arguments to the replace() function for the given string to replace the old value with a new value in a given string. Here count is a number indicating how many occurrences of the old value should be replaced.
- Store it in another variable.
- Print the above result i.e, The given string after replacing the old value with the given new value.
- The Exit of the program.
Below is the implementation:
# Give the string as user input using the input() function and store it in a variable. gvn_str = input("Enter some Random String = ") # Give the old value as user input using the input() function and store it in another variable. old_valu = input("Enter some Random String(oldvalue) = ") # Give the new value as user input using the input() function and store it in another variable. new_valu = input("Enter some Random String(newvalue) = ") # Give count as user input using the int(input()) function and # store it in another variable. gvn_cnt = int(input("Enter some random number = ")) # Pass the given old value, new value, count as arguments to the replace() # function for the given string to replace the old value with a new value # in a given string. Here count is a number indicating how many occurrences # of the old value should be replaced. # Store it in another variable. rslt = gvn_str.replace(old_valu, new_valu, gvn_cnt) # Print the above result i.e, The given string after replacing the old value # with the given new value. print("The given string after replacing oldvalue{", old_valu, "} with new value{", new_valu, "} is:") print(rslt)
Output:
Enter some Random String = welcome to to python to to programs Enter some Random String(oldvalue) = to Enter some Random String(newvalue) = all Enter some random number = 3 The given string after replacing oldvalue{ to } with new value{ all } is: welcome all all python all to programs
Go through our tutorial and learn about various Python String Method Examples and learn how to apply the knowledge while dealing with strings.