How to Convert Unicode to String in Python?

Estimated read time 2 min read

In Python, Unicode strings are represented by the str type, which is the default string type in Python 3. If you have a Unicode string and want to convert it to a regular string, you don’t need to do anything special because it’s already in the str format. Here’s an example:

unicode_string = "Hello, 世界!"
regular_string = unicode_string

print(regular_string)

In this example, the unicode_string contains a Unicode string. However, since it is already of type str, you can directly assign it to a variable of type str (regular_string in this case) without any conversion.

If you are working with Python 2, the default string type (str) is a byte string, and Unicode strings are represented by the unicode type. In that case, you can convert a Unicode string to a regular string (byte string) by encoding it using a specific encoding. Here’s an example:

unicode_string = u"Hello, 世界!"
regular_string = unicode_string.encode("utf-8")

print(regular_string)

In this Python 2 example, the unicode_string is a Unicode string prefixed with u. To convert it to a regular string, we use the encode() method and specify the desired encoding (in this case, "utf-8"). The encode() method returns a byte string, which is a regular string in Python 2.

Note that in Python 3, the encode() method is available for str objects as well, but it returns a byte string, not a regular string. Therefore, the encoding step is not necessary in Python 3 if you already have a str object representing a Unicode string.

Overall, whether you need to convert a Unicode string to a regular string depends on the Python version you are using and the specific string types involved.

You May Also Like

More From Author

+ There are no comments

Add yours

Leave a Reply