如何在python中从字符串的末尾删除字符串

时间:2025-05-08 14:36:38

引自:/u014636245/article/details/103120201

需要注意到是:

1. strip去除字符串两端给出的字符,如果想去除字符串末尾的字符用,请使用rstrip();如果想去除字符串开头的字符用,请使用lstrip().

2. ('指定字符串')删除字符串开头和结尾中指定的任何字符。但是组合以并不是将整个指定字符串看成一个组合。而是删除单个。

例如:

txt = "banana,,,,,ssqqqww....."

x = (",.qsw")# Remove the trailing characters if they are commas, s, q, or w:

print(x)

因此

domain='https:///'
# domain=domain[:-10]
print(('/'))

'''
结果输出:https://www.dytt8
当我想整个删除domin末尾'/',就不能用('/'),你看根据rstrip()的原理,删除字符串末尾的 /,i,n,d,e,x,.,c,o,m中的所有字符。其中 .net就是因为有 ., n,e,t所以也被删除了
'''