在Python中对不可变字符串进行更改

时间:2018-08-22 05:06:46

标签: python string

我需要以下代码的帮助:

def mutate_string(string, position, character):
    res=list(string)
    res[position] = 'character'
    s=''.join(res)
    print(s)

a = str(input())
b,c = input().split()

mutate_string(a,b,c)

当我尝试不使用拆分功能而将bc分别作为输入时,输出似乎很好,但是一行中的多个输入会引发以下错误:

abcdefg
5 h
Traceback (most recent call last):
File "C:/Users/Bhargav/.PyCharmCE2018.1/config/scratches/scratch_27.py", 
line 11, in <module>
mutate_string(a,b,c)
File "C:/Users/Bhargav/.PyCharmCE2018.1/config/scratches/scratch_27.py", 
line 3, in mutate_string
res[position] = 'character'
TypeError: list indices must be integers or slices, not str

0 个答案:

没有答案