Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python - how to delete hidden signs from string?

Tags:

python

Sometimes I have a strings with strange characters. They are not visible in browser, but are part of the string and are counted in len(). How can I get rid of it? Strip() deletes normal space but not that signs.

like image 224
robos85 Avatar asked Aug 22 '11 12:08

robos85


1 Answers

Use the character categories from the string module. If you want to allow all printable characters, you can do

from string import printable
new_string = ''.join(char for char in the_string if char in printable)

Building on YOU's answer, you can do this with re.sub too:

new_string = re.sub("[^{}]+".format(printable), "", the_string)

Also, if you want to see all the characters in a string, even the unprintable ones, you can always do

print repr(the_string)

which will show things like \x00 for unprintable characters.

like image 182
agf Avatar answered Oct 27 '22 14:10

agf