📌  相关文章
📜  如何在python代码示例中替换字符串的最后一个字符

📅  最后修改于: 2022-03-11 14:45:46.681000             🧑  作者: Mango

代码示例1
sample_string = "712345677890 173445667"
# if i wanted to do:
new_string=sample_string.replace("7", r)
print new_string
#you won't get your results
#Output:
r123456rr890 1r344566r
#but i wanted it to be
712345677890 17344566r
#there are different methods of solving this problem but i use this one
new_string=sample_string [:-1] + "r" # you can make change the amount of last letters getting replaced like for example the last three letters you would change [:-1] to [:-3]
#Output:
712345677890 17344566r #yay this is it