String rstrip()对字符串没有影响

String rstrip()对字符串没有影响,string,python-3.x,String,Python 3.x,尝试在最基本的级别上使用rstrip(),但似乎没有任何效果 例如: string1='text&moretext' string2=string1.rstrip('&') print(string2) 预期结果: 正文 实际结果: text&moretext 使用Python3,PyScripter 我缺少什么?someString.rstrip(c)删除字符串末尾出现的所有c。因此,例如 'text&&&&'.rstrip('&')

尝试在最基本的级别上使用rstrip(),但似乎没有任何效果

例如:

string1='text&moretext'
string2=string1.rstrip('&')
print(string2)
预期结果: 正文

实际结果: text&moretext

使用Python3,PyScripter

我缺少什么?

someString.rstrip(c)
删除字符串末尾出现的所有
c
。因此,例如

'text&&&&'.rstrip('&') = 'text'
'&'.join('Hello&World'.split('&')[:-1]) = 'Hello'
'&'.join('Hello&Python&World'.split('&')[:-1]) = 'Hello&Python'
也许你想要

'&'.join(string1.split('&')[:-1])
这会将分隔符“&”上的字符串拆分为一个字符串列表,删除最后一个字符串,然后使用分隔符“&”再次连接它们。因此,例如

'text&&&&'.rstrip('&') = 'text'
'&'.join('Hello&World'.split('&')[:-1]) = 'Hello'
'&'.join('Hello&Python&World'.split('&')[:-1]) = 'Hello&Python'

谢谢,太好了。请澄清一下您正在使用索引([:-1])做什么?删除列表中的最后一个成员。