Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 272
xsammy_boi Avatar asked Jan 09 '23 18:01

xsammy_boi


2 Answers

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

cool = "cool°"
cool = cool.replace("°","")
cool
'cool'
like image 151
Daniel Avatar answered Jan 11 '23 21:01

Daniel


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

cool = "cool°"

cool = cool.rstrip("°")
print(cool)
cool
like image 29
Padraic Cunningham Avatar answered Jan 11 '23 20:01

Padraic Cunningham