- Syntax
- Example 1: English Text
- Example 2: Hindi Text
- Example 3: Chinese Text
- Example 4: Emoji Characters
- Example 5: User Input
- Example 6: Unicode String Length
- Example 7: Accessing Unicode Characters
- Example 8: Combining Unicode Strings
- Example 9: Checking the Data Type
- Example 10: Mixing Different Languages
- Common Mistakes
- Best Practices
- Key Points to Remember
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.