如何从字符串中删除某些字符?[Python]
假设我有一个名为cool的字符串,cool设置为“cool°”如何从字符串中删除某些字符?[Python],python,string,character,Python,String,Character,假设我有一个名为cool的字符串,cool设置为“cool°” 如何从字符串中删除度符号?由于字符串是不可变的,请使用replace函数重新分配cool cool = "cool°" cool = cool.replace("°","") cool 'cool' 如果它们位于字符串末尾,请使用str.rstrip: cool = "cool°" cool = cool.rstrip("°") print(cool) cool cool = "cool°" cool = cool.rstr
如何从字符串中删除度符号?由于字符串是不可变的,请使用replace函数重新分配
cool
cool = "cool°"
cool = cool.replace("°","")
cool
'cool'
如果它们位于字符串末尾,请使用
str.rstrip
:
cool = "cool°"
cool = cool.rstrip("°")
print(cool)
cool
cool = "cool°"
cool = cool.rstrip("°")
print(cool)
cool