Home  >  Article  >  Backend Development  >  Can strings be changed in Python3?

Can strings be changed in Python3?

高洛峰
高洛峰Original
2017-03-08 09:44:581866browse

Can strings be changed in Python 3?

There is a method for changing strings: replace, for example:

a = 'lkjhgfdsa'
a.replace('l','123')'
123kjhgfdsa' #返回结果

As can be seen from the above example, str can also be changed. but! ! !

This change does not really change the original string, but is equivalent to creating a new string:

>>> a = 'lkjhgfdsa'
>>> b = a.replace('l','123')
>>> a
'lkjhgfdsa'
>>> b
'123kjhgfdsa'

From the above example, the value of a has not been changed. We copy the "modified" string to b. It can be seen that a and b are completely different.

Summary: Strings cannot be changed in Python 3. If you use the str.replace method to change a string, the original string will remain unchanged and a new changed string will be created.

For more related articles, can strings be changed in Python3, please pay attention to the PHP Chinese website!

Statement:
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