Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/306.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/string/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何从字符串中删除某些字符?[Python]_Python_String_Character - Fatal编程技术网

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

如何从字符串中删除某些字符?[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

假设我有一个名为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.rstrip("°")
print(cool)
cool