Is there a good shortcut for modifying python variables using methods?

后端 未结 2 2035
广开言路
广开言路 2021-01-29 14:45

i += 1 can be used instead of i = i + 1.

Is there a similar shorthand for string = string.replace(...)?

2条回答
  •  慢半拍i
    慢半拍i (楼主)
    2021-01-29 15:12

    No, because there is no built-in syntax for this.

    For a string, you can still use +=:

    >>> x = "hello"
    >>> x += " world"
    >>> x
    'hello world'
    

    Because += is defined for strings. There just isn't an assignment operator for replace.

提交回复
热议问题