Categories
Python Answers

How to convert a Unicode string that contains extra symbols to a string in Python?

Spread the love

Sometimes, we want to convert a Unicode string that contains extra symbols to a string in Python.

In this article, we’ll look at how to convert a Unicode string that contains extra symbols to a string in Python.

How to convert a Unicode string that contains extra symbols to a string in Python?

To convert a Unicode string that contains extra symbols to a string in Python, we can use the encode methods.

For instance, we write

a = u"aaaàçççñññ"
b = a.encode("ascii", "ignore")

to call a.encode with 'ascii' and 'ignore' to encode the string a as an ASCII string.

And we call it with 'ignore' to discard any characters that aren’t in the ASCII character set.

Conclusion

To convert a Unicode string that contains extra symbols to a string in Python, we can use the encode methods.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *