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

14,672

Solution 1

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

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

Solution 2

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

cool = "cool°"

cool = cool.rstrip("°")
print(cool)
cool
Share:
14,672
xsammy_boi
Author by

xsammy_boi

Updated on June 28, 2022

Comments

  • xsammy_boi
    xsammy_boi almost 2 years

    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?