简体   繁体   English

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

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

Say i have a string called cool and cool is set to "cool°" 假设我有一个名为cool的字符串,cool设置为“cool°”

cool = "cool°"

how do i remove the degree symbol from the string? 如何从字符串中删除度数符号?

Since strings are immutable, use the replace function to reassign cool 由于字符串是不可变的,因此使用replace函数重新分配cool

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

If they are at the end of the string use str.rstrip : 如果它们位于字符串的末尾,请使用str.rstrip

cool = "cool°"

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

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

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