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

后端 未结 2 2031
广开言路
广开言路 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条回答
  •  抹茶落季
    2021-01-29 15:12

    The type string supports the += operator. You can write str += "hello" but such shortening does not work for functions. The type string is immutable

    A function like string.replace returns a new string and therefore you have to replace it against your old string (if that's what you want).

提交回复
热议问题