Home > Backend Development > Python Tutorial > How to replace the contents of a string in python

How to replace the contents of a string in python

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
Release: 2024-03-01 22:55:02
forward
937 people have browsed it

How to replace the contents of a string in python

pythonYou can use the replace() function to replace the contents of the string. replace()The function accepts two parameters. The first parameter is the string to be replaced, and the second parameter is the string to be replaced. Examples are as follows:

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

The output result is:

Hello, Python!
Copy after login
Copy after login

Note that the replace() function returns the new string after replacement, and the original string will not be modified. If you want to perform a replacement on the original string, you can assign it directly to the original string variable. For example:

string = "Hello, World!"
string = string.replace("World", "Python")
print(string)
Copy after login

The output result is:

Hello, Python!
Copy after login
Copy after login

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

Related labels:
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template