In Python, you can change a string or part of it using the replace() method from the str class. The replace() method needs two things: the old string you want to change and the new string you want to use.
In this tutorial, we’ll learn how to replace parts of a string in Python and how to replace parts of strings in a list.
Replace a Substring in Python
Here’s an example of how to use the replace() method to change a string in Python:
# Original string
string = “Hello, World!”
# Replace the word “Hello” with “Hi”
new_string = string.replace(“Hello”, “Hi”)
# Print the modified string
print(new_string) # Outputs: “Hi, World!”
In this example, we have a string called string with the text “Hello, World!â€. We use the replace() method to change “Hello†to “Hi†and store the new string in the new_string variable. Finally, we print the new string.
You can also use the replace() method to change all instances of a string at once. For example:
# Original string
string = “Hello, World! Hello, Python! Hello TecAdmin!”
# Replace all occurrences of the word “Hello” with “Hi”
new_string = string.replace(“Hello”, “Hi”)
# Print the modified string
print(new_string) # Outputs: “Hi, World! Hi, Python! Hi, TecAdmin”
In this example, we have a string with three instances of the word “Helloâ€. The replace() method changes all of them to “Hiâ€.
Replace Substring in a List of Strings
In Python, you can use a for loop to go through a list of strings and use the replace() method to change a substring in each string.
Here’s an example of how to replace a substring in a list of strings:
# List of strings
strings = [“Hello, World!”, “Hello, Python!”]
# Iterate over the list of strings
for i, s in enumerate(strings):
# Replace the substring “Hello” with “Hi”
strings[i] = s.replace(“Hello”, “Hi”)
# Print the modified list of strings
print(strings) # Outputs: [“Hi, World!”, “Hi, Python!”]
In this example, we have a list of strings called strings. We use a for loop to go through the list. Inside the loop, we use the replace() method to change “Hello†to “Hi†in each string, then save the new string back into the list. Finally, we print the new list of strings.
You can also use a list comprehension to do the same thing in one line of code:
# List of strings
strings = [“Hello, World!”, “Hello, Python!”, “Hello, TecAdmin!”]
# Use a list comprehension to replace the substring “Hello” with “Hi” in each string
new_strings = [s.replace(“Hello”, “Hi”) for s in strings]
# Print the modified list of strings
print(new_strings) # Outputs: [“Hi, World!”, “Hi, Python!”, “Hi, TecAdmin!”]
In this example, we have a list of strings called strings. We use list comprehension to make a new list where “Hello†is changed to “Hi†in each string. The replace() method does the replacing, and we save the new list in the new_strings variable. Finally, we print the new list.
Conclusion
In this tutorial, you learned two ways to replace parts of a string in Python. The first method uses the replace() function, and the second method uses list comprehension.
The post How to Replace a String in Python appeared first on TecAdmin.
Source: Read More