[英]Replace character to "@" in string
Why python does not replace last character for @?为什么 python 不替换 @ 的最后一个字符?
str_manip = input("Enter a sentence ")
last_char = str_manip[-1]
print(last_char)
change_char = str_manip.replace("last_char", "@")
print(change_char)
It comes up as exactly the same sentence as I enter, unchanged.它出现的句子与我输入的完全相同,没有变化。
I have read a few websites and still do not understand.看了几个网站还是不明白。 I thought maybe it was because @ is char but str() doesn't help, saving it as separate string variable also doesn't work.
我想也许是因为 @ 是 char 但 str() 没有帮助,将它保存为单独的字符串变量也不起作用。
Try this尝试这个
str_manip = input("Enter a sentence ")
last_char = str_manip[-1]
print(last_char)
change_char = str_manip.replace(last_char, "@")
print(change_char)
Replace代替
change_char = str_manip.replace("last_char", "@")
by经过
change_char = str_manip.replace(last_char, "@")
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.