Home  >  Article  >  Backend Development  >  How to replace a specified string in python

How to replace a specified string in python

WBOY
WBOYforward
2024-03-02 09:10:201121browse

How to replace a specified string in python

In python, you can use the replace() method of string to replace the specified string. This method accepts two parameters, the first parameter is the string to be replaced, and the second parameter is the replaced string. Here is an example:

string = "Hello, World!"
new_string = string.replace("World", "Python")
print(new_string)

The output result is:

Hello, Python!

In this example, we replace "World" in the string Hello, World! with "Python" and print the result.

The above is the detailed content of How to replace a specified string in python. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:lsjlt.com. If there is any infringement, please contact admin@php.cn delete