Skip to main content
 首页 » 编程设计

python之如何从字符串中删除所有尾随破折号

2025年05月04日20grandyang

例子:

string1 = 'title--' 
string2 = 'title-' 
string3 = 'this-is-a-title----' 
 
>> print doSomething(string1) 
>> title 
 
>> print doSomething(string2) 
>> title 
 
>> print doSomething(string3) 
>> this-is-a-title 

请您参考如下方法:

string1.rstrip("-") 
# "title" 
string2.rstrip("-") 
# "title" 
string3.rstrip("-") 
# "title-is-a-title"