替换字符

基本类型 / 字符串

text = "1-3-2"

# Through a list of characters
chars = list(text)
chars[2], chars[4] = chars[4], chars[2]
text = "".join(chars)
# text is '1-2-3'

print(f"{text = }")

# Strings are immutable
try:
    text[2] = "2"
except TypeError as error:
    print(f"{error = }")