如何从字符串中删除某些字符? [蟒蛇]

时间:2015-03-06 23:32:39

标签: python string character

假设我有一个名为cool的字符串,cool设置为“cool°”

cool = "cool°"

如何从字符串中删除度数符号?

2 个答案:

答案 0 :(得分:4)

由于字符串是不可变的,因此请使用replace函数重新分配cool

cool = "cool°"
cool = cool.replace("°","")
cool
'cool'

答案 1 :(得分:3)

如果它们位于字符串的末尾,请使用str.rstrip

cool = "cool°"

cool = cool.rstrip("°")
print(cool)
cool