sentence = input ('Please enter a sentce: ')
change = input ('What word do you want to change: ')
replace = input ('What do you want to replace it with: ')
n_s = replace(change,replace)
print (n_s)
我有这个,但当我运行它说
n_s = replace(change,replace) TypeError:'str' object is not callable
方法replace()返回一个字符串的副本,在该字符串中出现的旧字符串已被替换为新字符串,可以选择将替换次数限制为最大值
语法:
str.replace(old, new[, max])
下面是针对python3的更正代码。请注意,print语句前面最后一行的语法是n\u s=句子.替换(更换)
你想要:
n_s = sentence.replace(change, replace)
。它会给您一个类型错误,因为名为replace
的变量是一个字符串,您试图像方法一样调用它。你知道吗相关问题 更多 >
编程相关推荐