Unicode String in Python

In Python, Unicode strings allow you to work with characters from various languages and special symbols. While Python 2 required the u prefix for Unicode strings, Python 3 treats all strings as Unicode by default.

Unicode in Python 3

In Python 3, all strings are Unicode by default, so no special prefix is needed ?

# All strings are Unicode in Python 3
text = 'Hello, world! ?? ?????'
print(text)
print(type(text))
Hello, world! ?? ?????
<class 'str'>

Working with Unicode Characters

You can use Unicode escape sequences to represent special characters ?

# Using Unicode escape sequences
heart = '\u2764'  # Heart symbol
smiley = '\U0001F600'  # Grinning face emoji
greek = '\u03B1\u03B2\u03B3'  # Greek letters alpha, beta, gamma

print(f"Heart: {heart}")
print(f"Smiley: {smiley}")
print(f"Greek: {greek}")
Heart: ?
Smiley: ?
Greek: ???

Encoding and Decoding

Convert between Unicode strings and bytes using encoding methods ?

# Encoding Unicode to bytes
text = "Hello ??"
encoded = text.encode('utf-8')
print(f"Original: {text}")
print(f"Encoded: {encoded}")

# Decoding bytes back to Unicode
decoded = encoded.decode('utf-8')
print(f"Decoded: {decoded}")
Original: Hello ??
Encoded: b'Hello \xe4\xb8\x96\xe7\x95\x8c'
Decoded: Hello ??

Checking String Properties

Use built-in methods to analyze Unicode strings ?

text = "Hello123???"

print(f"Is alphanumeric: {text.isalnum()}")
print(f"Is alpha: {text.isalpha()}")
print(f"Is ASCII: {text.isascii()}")
print(f"Length: {len(text)}")
Is alphanumeric: True
Is alpha: False
Is ASCII: False
Length: 11

Conclusion

Python 3 handles Unicode seamlessly with all strings being Unicode by default. Use encoding/decoding methods when working with bytes, and Unicode escape sequences for special characters.

Updated on: 2026-03-25T07:33:16+05:30

3K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements