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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With