替换子串

基本类型 / 字符串

start_string = "3, 2, 1, go!"
start_string = (start_string
               .replace("1", "one")
               .replace("2", "two")
               .replace("3", "three"))
# start_string is 'three, two, one, go!'

# Replaces all occurrences
one_string = "1 1 1"
all_replaced = one_string.replace("1", "one")
# all_replaced is 'one one one'

# The third argument limits the count
first_only = one_string.replace("1", "one", 1)
# first_only is 'one 1 1'

print(f"{start_string = }")
print(f"{all_replaced = }")
print(f"{first_only = }")