Akshay
Akshay

Reputation: 94

How to remove these special ascii characters from string in python?

string='I’m celebrating my sixth month anniversary of no longer being a customer of Star Wars. I’ve saved a lot of money'

From the above string i want to get rid of all the special characters.

Upvotes: 0

Views: 849

Answers (1)

blhsing
blhsing

Reputation: 106553

If you simply want to remove all non-ASCII characters from string:

print(''.join(c for c in string if ord(c) < 128))

This outputs:

Im celebrating my sixth month anniversary of no longer being a customer of Star Wars. Ive saved a lot of money

However, it's better that you find out what the encoding of your original string is and use bytes.decode to convert it to its original content with nothing removed.

Upvotes: 8

Related Questions