python中可以使用rstrip()方法刪除string字符串末尾的指定字符(默認為空格)。
rstrip()方法語法:str.rstrip([chars])
chars--指定刪除的字符(默認為空格)
返回值:返回刪除string字符串末尾的指定字符后生成的新字符串。
示例:
#!/usr/bin/python3
S="thisisstringexample....wow!!!"
print(S.rstrip())
S="*****thisisstringexample....wow!!!*****"
print(S.rstrip('*'))
輸出結果如下:
thisisstringexample....wow!!!
*****thisisstringexample....wow!!!
以上內容為大家介紹了python字符串不要最后字符怎么做?希望對大家有所幫助,如果想要了解更多Python相關知識,請關注IT培訓機構:千鋒教育。