• Home
  • 4.26 Unicode Strings

4.26 Unicode Strings

View Categories

4.26 Unicode Strings

2 min read

A Unicode string is a string that can store characters from almost every writing system in the world, including English, Hindi, Chinese, Japanese, Arabic, emojis, mathematical symbols, and many other special characters.

In Python 3, all strings are Unicode by default. This means you can directly store and manipulate international text without using any special syntax.

Unicode allows programs to work correctly with multilingual data, making Python suitable for developing applications used worldwide.


Syntax #

text = "Unicode characters"

Components #

Component Description
text Variable storing the Unicode string.
" " Quotes enclosing the string.
Unicode characters Any valid Unicode characters.

Example 1: English Text #

main.py

text = "Python Programming"

print(text)

Output #

Python Programming

Explanation #

  • The string contains English characters.
  • English letters are Unicode characters.
  • Python stores the string as Unicode.

Example 2: Hindi Text #

main.py

text = "เคจเคฎเคธเฅเคคเฅ‡"

print(text)

Output #

เคจเคฎเคธเฅเคคเฅ‡

Explanation #

  • The string contains Hindi characters.
  • Python supports Unicode characters directly.
  • No special configuration is required.

Example 3: Chinese Text #

main.py

text = "ไฝ ๅฅฝ"

print(text)

Output #

ไฝ ๅฅฝ

Explanation #

  • The string contains Chinese characters.
  • Python stores and prints them correctly.
  • Unicode makes multilingual programming possible.

Example 4: Emoji Characters #

main.py

message = "Python โค๏ธ"

print(message)

Output #

Python โค๏ธ

Explanation #

  • Emojis are also Unicode characters.
  • Python treats them like any other character.
  • They can be stored and displayed in strings.

Example 5: User Input #

main.py

name = input("Enter your name: ")

print("Hello,", name)

Sample Input #

เคฐเคพเคœ

Output #

Hello, เคฐเคพเคœ

Explanation #

  • The user enters a Unicode string.
  • Python stores the entered text correctly.
  • The output displays the Unicode characters without modification.

Example 6: Unicode String Length #

main.py

text = "เคจเคฎเคธเฅเคคเฅ‡"

print(len(text))

Output #

6

Explanation #

  • len() counts the number of Unicode characters in the string.
  • "เคจเคฎเคธเฅเคคเฅ‡" contains six Unicode characters.
  • The returned value is an integer.

Example 7: Accessing Unicode Characters #

main.py

text = "ไฝ ๅฅฝ"

print(text[0])
print(text[1])

Output #

ไฝ 
ๅฅฝ

Explanation #

  • Unicode strings support indexing just like ordinary strings.
  • Each character can be accessed using its index.
  • Python treats Unicode characters as string elements.

Example 8: Combining Unicode Strings #

main.py

greeting = "เคจเคฎเคธเฅเคคเฅ‡"
name = "เคฐเคพเคœ"

print(greeting + " " + name)

Output #

เคจเคฎเคธเฅเคคเฅ‡ เคฐเคพเคœ

Explanation #

  • Unicode strings can be concatenated.
  • The space is added between the two strings.
  • The result is a new Unicode string.

Example 9: Checking the Data Type #

main.py

text = "ใ“ใ‚“ใซใกใฏ"

print(type(text))

Output #

<class 'str'>

Explanation #

  • Unicode strings are still normal Python strings.
  • Their data type is str.
  • Python 3 does not have a separate Unicode string type.

Example 10: Mixing Different Languages #

main.py

text = "Hello เคจเคฎเคธเฅเคคเฅ‡ ไฝ ๅฅฝ"

print(text)

Output #

Hello เคจเคฎเคธเฅเคคเฅ‡ ไฝ ๅฅฝ

Explanation #

  • A single string can contain characters from multiple languages.
  • Python stores all of them as Unicode.
  • This makes multilingual applications easy to develop.

Common Mistakes #

1. Assuming Unicode Requires Special Syntax #

Incorrect

text = "เคจเคฎเคธเฅเคคเฅ‡"

print(text)

Incorrect Expectation #

SyntaxError

Actual Output #

เคจเคฎเคธเฅเคคเฅ‡

Reason

Python 3 strings are Unicode by default.


2. Assuming Unicode Has a Different Data Type #

Incorrect

text = "ไฝ ๅฅฝ"

print(type(text))

Incorrect Expectation #

<class 'unicode'>

Actual Output #

<class 'str'>

Reason

Unicode strings are of type str in Python 3.


3. Assuming Emojis Cannot Be Stored #

Incorrect

text = "๐Ÿ˜Š"

print(text)

Incorrect Expectation #

Error

Actual Output #

๐Ÿ˜Š

Reason

Emojis are valid Unicode characters.


4. Assuming Unicode Cannot Be Indexed #

Incorrect

text = "เคจเคฎเคธเฅเคคเฅ‡"

print(text[0])

Incorrect Expectation #

IndexError

Actual Output #

เคจ

Reason

Unicode strings support indexing just like other strings.


Best Practices #

  • Store multilingual text directly as Unicode strings.
  • Use meaningful variable names when working with Unicode data.
  • Test applications using text from different languages when developing international software.
  • Remember that Unicode strings support all normal string operations.
  • Ensure your editor saves source files using UTF-8 encoding.

Key Points to Remember #

  • Python 3 strings are Unicode by default.
  • Unicode supports characters from almost every language.
  • Emojis and special symbols are also Unicode characters.
  • Unicode strings are of type str.
  • Unicode strings support indexing, slicing, concatenation, and all standard string methods.
  • No special syntax is required to create Unicode strings.
  • Unicode makes Python suitable for multilingual applications.

Powered by BetterDocs

Leave a Reply

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