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

How to remove certain characters from a string? [Python]

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

cool = "cool°"

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

由于字符串是不可变的,使用替换函数重新赋值cool

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

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

cool = "cool°"

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