Converting Unicode Strings to Regular Strings in Python

Convert A Unicode String To A String In Python (containing Extra Symbols)

In python, many messages including errors or inputs and outputs are internationalized so that people who do not know English can also program in it. Unicode is a format in which various characters in a string are given a unique identity. It is a type of specification that assigns each character used in human languages a specific value or code.

Unicode is a format that assigns each character used in human languages a unique code. It’s essential for maintaining uniformity and accommodating special characters in programming languages. The Unicode library is continuously updated with the latest characters and symbols

In the Unicode standard, characters (the smallest components of a string) are represented as code points. A code point can have any value between 0 and 0x10FFFF (about 1.1 million values). Therefore, a Unicode string is a collection of code points.

Unicode characters can be converted into 8-bit bytes and this process can be done in the memory of a computer system, also known as, encoding.

Benefits of UTF-8 Encoding

“UTF” stands for Unicode Transformation format and the “8” represents the 8-bit values that are used in this format for encoding. It has a number of advantages over using the 32 bit encoding in a system’s CPU such as it can handle code point values and also ASCII strings are also valid utf-8 text.

Byte-ordering issues are resolved when utf-8 encoding is used. Lost data can be resynchronized and it is possible to determine the start and end points of utf-8 texts. It is also portable unlike the 32 bit encoding which was not portable.

This method is now the standard Unicode encoding for python.

Also check out: Converting Base-2 Binary Number Strings to Integers in Python.

Converting normal string to Unicode strings

We can decode a Unicode string to a normal one using in-built libraries in python. We will take a look at it in this article and you can use whichever you see fit in your programs.

Example: Converting a String to Unicode Characters

Let’s first convert a string into Unicode characters.

import re 
# initializing string
org = 'Askpythonisthebest'
sol = (re.sub('.', lambda x: r'\u % 04X' % ord(x.group()), org)) 
# printing result
print("The unicode converted String : " + str(sol))

Our output is:

The unicode converted String : \u  041\u  073\u  06B\u  070\u  079\u  074\u  068\u  06F\u  06E\u  069\u  073\u  074\u  068\u  065\u  062\u  065\u  073\u  074

Converting Unicode strings to normal strings

We can transform Unicode strings into normal strings using the unicode.normalize() function from the unicodedata module. The module uses the same conventions in the Unicode characters database.

The syntax of the function is:

unicodedata.normalize('type', string_name)

The ‘type’ parameter can take up 4 different values: “NFC”,”NFKC”,”NFD” and “NFKD”. For each character there are two normal forms: D which stands for normal canonical decomposition(NFD) and C which first performs normal canonical decomposition and then again composes pre-combined characters again(NFC).

The normal form “NFKD” applies normal compatibility decomposition whereas the normal form “NFKC” first applies normal compatibility decomposition, then canonical composition.

Two Unicode strings may appear the same to a human eye but if one has combining characters and the other one doesn’t, then they may not compare equal.

Example: Converting Unicode Strings to Regular Strings

Let’s look at one small example:

#importing the unicodedata module
import unicodedata 
#initializing unicode string
org=u"Askpython.com!"
#converting the unicode string using normalize()
ans= unicodedata.normalize('NFC', org)
#printing the type after coversion
print("The string and it's type is= ")
print(ans, type(ans))

The output will be:

The string and it's type is= 
Askpython.com! <class 'str'>
Convert A Unicode String To A String In Python Containing Extra Symbols 1
Converting A Unicode String To A String In Python.

Let’s take another example, this time we will use the encode() function along with the normalize() function to take care of more than one special characters.

#importing the unicodedata module
import unicodedata 
#initializing unicode string
org=u"üft träms inför på fédéral große-aàççññ"
#converting the unicode string using normalize()
ans= unicodedata.normalize('NFKD', org).encode('ascii', 'ignore')
#printing the type after coversion
print("The string and it's type is= ")
print(ans, type(ans))

The output would be:

The string and it's type is= 
b'uft trams infor pa federal groe-aaccnn' <class 'bytes'>
Special Character Conversion From Unicode To Normal String
Special Character Conversion From Unicode To Normal String

Suggested: Converting Bytes to Ascii or Unicode.

Conclusion

The Unicode character database provides a universal method for assigning unique values to various characters, making it easy to identify them in computer systems. Although most Unicode and ASCII encoding and decoding happen behind the scenes, it’s essential to understand the mechanisms and rules for converting characters to their Unicode counterparts. In this tutorial, we’ve demonstrated how to convert Unicode strings to regular strings in Python with ease.