从字符串中删除尾随逗号可以通过以下几种方法实现:
string = "abc,def,ghi,"
new_string = string[:-1] if string.endswith(",") else string
print(new_string)
输出结果为:abc,def,ghi
import re
string = "abc,def,ghi,"
new_string = re.sub(r',$', '', string)
print(new_string)
输出结果为:abc,def,ghi
string = "abc,def,ghi,"
new_string = string.rstrip(",")
print(new_string)
输出结果为:abc,def,ghi
以上是三种常见的方法,可以根据具体需求选择适合的方法来删除字符串中的尾随逗号。
领取专属 10元无门槛券
手把手带您无忧上云