简体   繁体   中英

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

Say i have a string called cool and cool is set to "cool°"

cool = "cool°"

how do i remove the degree symbol from the string?

Since strings are immutable, use the replace function to reassign cool

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

If they are at the end of the string use str.rstrip :

cool = "cool°"

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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