繁体   English   中英

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

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

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

cool = "cool°"

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

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

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

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

cool = "cool°"

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM