'in-place' string modifications in Python

后端 未结 14 1501
一整个雨季
一整个雨季 2020-12-05 23:38

In Python, strings are immutable.

What is the standard idiom to walk through a string character-by-character and modify it?

The only methods I can think of a

14条回答
  •  执念已碎
    2020-12-05 23:54

    you can use the UserString module:

     >>> import UserString
    ... s = UserString.MutableString('Python')
    ... print s
    Python
    >>> s[0] = 'c'
    >>> print s
    cython
    

提交回复
热议问题