Python

P
PythonHub
Progress 0%

Python

  • Home
  • History of Python
  • Applications of Python
  • Introduction To Python
    • What is Python
    • Character Set
    • Tokens in Python
    • Python Execution Mode
    • Variable And Identifiers
    • Data Types in Python
    • Operators And Expressions
    • Constants in Python
    • Assignment Statement
    • Input / Output in Python
    • Simple Python Scripts
    • Namespace in Python
    • πŸ“ Assignments
  • Operators in Python
    • Arithmetic Operators
    • Assignment Operators
    • Shorthand Assignment Operators
    • Relational Operators
    • Logical Operators
    • Bitwise Operators
    • Special Operators
    • πŸ“ Assignments
  • Input Output in Python
    • Accept Input
    • Output Formatting
    • πŸ“ Assignments
  • Conditional Statement
    • Decision Making
    • if Statement
    • IF-ELSE STATEMENT
    • IF-ELSE LADDER
    • NESTED IF-ELSE
    • Short Hand IF-ELSE
    • πŸ“ Assignments
  • Loops
    • Introduction to Loops
    • While Loop
    • Nested While Loop
    • πŸ“ While Loop Assignments
    • For Loop
    • For Loop Examples
    • Nested For Loop
    • Nested For Loop Examples
    • Infinite While Loops
    • Infinite For Loops
    • Break, Continue and Else in Loops
    • Difference Between For and While Loop
    • For Each Loop
    • πŸ“ For Each Assignments
    • πŸ“ All Loops Assignments
  • List
    • List in Python
    • Access List Elements
    • List Functions
    • Iterate (Loop) List
    • List Comprehension
    • πŸ“ Assignments
  • Tuple
    • Tuple in Python
    • Access Tuple Elements
    • Tuple Functions
    • Iterate (Loop) Tuple
    • Unpack Tuple
    • Tuple Comprehension
    • πŸ“ Assignments
  • Set
    • Set in Python
    • Access Set Elements
    • Set Methods
    • Iterate (Loop) Set
    • Pack/Unpack Set
    • Set Comprehension
    • πŸ“ Assignments
  • Dictionary
    • Dictionary
    • Access Dictionary Items
    • Dictionary Methods
    • Iterate (Loop) Dictionary
    • Formatting Dictionaries
    • Nested Dictionaries
    • Dictionary Comprehension
    • πŸ“ Assignments
  • Diff List Tuple Set Dictionary
    • List vs Tuple
    • List vs Set
    • List vs Dictionary
    • Tuple vs Set
    • Tuple vs Dictionary
    • Dictionary vs Set
    • πŸ“ Assignments
  • Exception
    • Error vs Exception
    • Exception Handling
    • Types of Exception
    • User Defined Exception
    • Logging Exception
    • πŸ“ Assignments
  • Functions
    • Introduction to Functions
    • Modular Programming
    • Types of Functions
    • Inbuilt Functions
    • Need For User-Defined Function
    • Elements of User Defined Function
    • Function Arguments
    • Nesting of Functions
    • Recursion
    • Global Local and Non Local
    • Python Lambda Functions
    • πŸ“ Assignments
  • Python Module
    • Introduction to Module
    • Inbuilt Modules in Python
    • User Defined Module
    • πŸ“ Assignments
  • File Handling
    • Introduction to Files
    • Create File
    • Read Files
    • Write to File
    • Rename File
    • Copy File
    • Move File
    • List Files in Directory
    • Binary Files
    • Zipping and Unzipping Files
    • πŸ“ Assignments
  • Strings
    • Basics of Strings
    • String Special Operators
    • String Formatting Operators
    • String Methods
    • πŸ“ Assignments
  • Regular Expressions
  • Python OOPS
    • Basics of Object Oriented
    • What are Classes and Objects?
    • Creating Class and Object
    • OOP vs Procedural Programming
    • Difference Between Classes and Objects
    • Constructors
    • Destructor
    • Built Class Methods and Attributes
    • Class and Instance Variables
    • Inheritance in Python
    • Single Inheritance
    • Multiple Inheritance
    • Multilevel Inheritance
    • Hierarchical Inheritance
    • Hybrid Inheritance
    • Abstraction
    • Method Overriding
    • Abstract Method
    • Interfaces in Python
    • Abstract Class vs Interface
    • Public, Private and Protected
    • Overloading vs Overriding
    • Inheritance vs Composition
    • Encapsulation
    • Polymorphism
    • Inner Classes
    • πŸ“ Assignments
  • Advanced Python
    • Iterator in Python
    • Generator in Python
    • Decorator in Python
    • Type Hints in Python
    • Async/Await Programming
    • Dataclasses in Python
    • Context Managers in Python
    • Property Decorator in Python
    • __slots__ in Python
    • Enums in Python
    • Walrus Operator in Python
    • Match-Case in Python
    • πŸ“ Assignments
  • Python Standard Library
    • Collections Module
    • Itertools Module
    • Functools Module
    • Datetime Module
    • JSON Module
    • OS Module
    • Sys Module
    • Random Module
    • Math Module
    • πŸ“ Assignments
  • Python Testing
    • Unit Testing in Python
    • Pytest Framework
    • Mocking in Python
    • πŸ“ Assignments
  • Python Best Practices
    • PEP 8 Style Guide
    • Docstrings in Python
    • Logging in Python
    • Code Optimization Tips
    • Debugging Techniques
    • πŸ“ Assignments
  • MySQL Database in Python
    • Introduction to MySQL with Python
    • DBMS vs File System
    • Connecting to MySQL Database
    • Create Database in MySQL
    • Create Table in MySQL
    • Insert Data in MySQL
    • Insert Multiple Rows
    • Select Data from MySQL
    • WHERE Clause in MySQL
    • Update Data in MySQL
    • Delete Data from MySQL
    • Parameterized Queries
    • Transaction Management
    • Error Handling
    • Connection Pooling
    • MySQL Drivers Guide
    • Joins in MySQL
    • Aggregation Functions
    • Backup MySQL Database
    • Best Practices
    • πŸ“ Assignments
  • MySQL Database Operations
    • SELECT Statement
    • MySQL Operators
    • DDL Statements
    • DML Statements
    • Subqueries
    • JOIN Operations
    • Aggregation
    • Case Study
    • πŸ“ Assignments
  • Graphics in Python
  • Threads in Python
    • Introduction to Threads
    • Process vs Threads
    • Concurrent Programming & GIL
    • Uses of Threads
    • Creating Threads
    • Single Tasking
    • Multi Tasking
    • Thread Synchronization
    • πŸ“ Assignments
  • Interview Questions & Answers
  • Python Case Studies
  • Multiple Choice Questions
  • πŸ“ Practice Problems
Get in Touch
  • tech2dsm@gmail.com

© Sankalan Data Tech

Python Language Interactive Tutorial

πŸ”€ Character Set in Python

Python Character Set - Letters, Digits, Special Symbols

Understanding the building blocks of Python: The Character Set

Created by Sankalan Data Tech Team Verified
Data Engineers, Analysts, Scientists & Trainers
We are a team of Python developers, data engineers and data scientists with years of real-world experience. We have built production applications. We have tackled complex problems. We have helped teams ship better code. What drives us? Teaching β€” especially that moment when things finally click. Our tutorials focus on practical examples and honest guidance. Whether you are just starting out or leveling up we are here to make Python genuinely useful.
πŸ“‘ On this page:
  • What is a Character Set?
  • Why is the Character Set Important?
  • History & Evolution of Character Sets
  • Categories of Python Characters
  • Letters (Alphabets)
  • Digits (Numerals)
  • Special Symbols
  • Whitespace Characters
  • Python vs Other Languages
  • Character Encoding in Python
  • Common Mistakes
  • Quick Reference
  • Try It Yourself
  • Test Your Knowledge
  • FAQ
  • Learning Resources
Share this tutorial:
Twitter LinkedIn Facebook WhatsApp Reddit Telegram Email Copy Link
πŸ“š What You'll Learn Here
  • What is a Character Set - Understanding the building blocks of Python
  • Letters (Alphabets) - Uppercase and lowercase letters in Python
  • Digits (Numerals) - Numeric characters and their use
  • Special Symbols - Punctuation and special characters
  • Whitespace Characters - Spaces, tabs, and newlines
  • Character Encoding - How Python stores characters
  • And More - Common mistakes, examples, and interactive practice

What is a Character Set?

Before we write our first Python program, we need to understand the character set β€” the alphabet of the Python language. Just like you need to know the English alphabet before you can write a sentence, you need to know the character set before you can write Python code.

A character set is a collection of all the characters that Python recognizes and can work with. When you type a program, Python reads every character you type β€” letters, numbers, punctuation, spaces β€” and interprets them based on rules it was designed to follow.

Think of it this way: Python is like a very smart assistant. You give it instructions using a language it understands. The character set defines all the symbols you can use to give those instructions. If you use a character that's not in Python's character set, Python won't understand you β€” just like if you spoke a language someone didn't know.

πŸ’‘ Did you know? Python's character set is based on Unicode, which means it can represent characters from almost every written language in the world β€” from English to Hindi to Chinese!

Why is the Character Set Important?

Understanding the character set might seem trivial at first, but it's actually critical for writing correct Python code. Here's why:

βœ… Valid Code

Using the correct characters ensures your code is syntactically valid. Python won't even start running your program if it contains characters it doesn't recognize.

πŸ“– Readability

Knowing which characters are allowed helps you write clean, readable code. It also helps you understand other people's code.

πŸ”€ Unicode Support

Python's character set includes Unicode, which means you can write programs that work with text in any language. This is huge for global applications!

The Evolution of Character Sets

Character sets have a fascinating history. In the early days of computing, different systems used different character sets, which made sharing data difficult.

Originally, computers used ASCII (American Standard Code for Information Interchange), which could represent only 128 characters β€” enough for English letters, digits, and a few symbols. But as computers became global, ASCII wasn't enough.

That's where Unicode came in. Unicode can represent over 143,000 characters from all the world's writing systems. Python adopted Unicode early, which is why Python programs can work with text in any language right out of the box.

πŸ“… Character Set Timeline

1960s

ASCII introduced (128 characters)

1980s

Extended ASCII (256 characters)

1991

Unicode 1.0 released

2008

Python 3.0 uses Unicode by default

2024

Python supports all Unicode characters

πŸ“ˆ Fun fact: Python 3.0 (released in 2008) made Unicode the default character encoding. This was a game-changer that made Python the language of choice for international applications.

Categories of Python Characters

Python's character set can be divided into four main categories. Let's explore each one in detail:

πŸ”€ Letters

Uppercase (A-Z) and lowercase (a-z) English letters. These are used to form variable names, function names, keywords, and more.

πŸ”’ Digits

Numeric characters (0-9). Used for numeric literals, indexing, and mathematical operations.

πŸ”£ Special Symbols

Punctuation and special characters like +, -, *, /, =, %, @, &, etc. Used for operators, syntax, and formatting.

πŸ“ Whitespace

Spaces, tabs, newlines, and other invisible characters. Used for indentation and formatting code.

Letters (Alphabets)

Letters are the most basic building blocks of any programming language. Python supports all English letters:

Type Characters Example Usage
Uppercase A B C D E F G H I J K L M N O P Q R S T U V W X Y Z MyVariable = 10
Lowercase a b c d e f g h i j k l m n o p q r s t u v w x y z my_variable = 10
Unicode Γ‘, Γ©, Γ­, Γ³, ΓΊ, Γ±, ΓΌ, ... cafΓ© = "coffee"

Key rule: Python is case-sensitive. That means MyVar and myvar are different variables. This is one of the most common sources of errors for beginners!

Digits (Numerals)

Digits are numeric characters used to represent numbers in Python. Python supports the standard 10 digits:

Digits Example Explanation
0 1 2 3 4 5 6 7 8 9 age = 25 Digits can be used in variable names (but not as the first character)
Unicode Digits num = Ω‘Ω’Ω£ Python supports digits from other scripts (Arabic, etc.)

Important: While digits can be part of variable names (like var1), they cannot be the first character of a variable name. So 1var is invalid, but var1 is perfectly fine.

Special Symbols

Special symbols are characters that have special meanings in Python. They're used for operators, punctuation, and syntax. Here are the most important ones:

Symbol Name Example Usage
+ - * / Arithmetic Operators result = 5 + 3
= Assignment Operator x = 10
== != < > <= >= Comparison Operators if x == 10:
( ) [ ] { } Brackets & Braces my_list = [1,2,3]
: ; . , Punctuation def my_func():
# @ & | ^ ~ Special Operators # This is a comment

Whitespace Characters

Whitespace characters are invisible but crucial. Unlike some languages that ignore whitespace, Python uses it to determine code structure. This is one of Python's most distinctive features.

Character Name Python Representation Purpose
Space ' ' Separates tokens, used for indentation
\t Tab '\t' Used for indentation (but avoid mixing with spaces)
\n Newline '\n' Ends a line of code
\r Carriage Return '\r' Used on Windows systems (with \n)

⚠️ Critical: Python uses indentation to define code blocks. Other languages use braces {} or BEGIN/END, but Python uses whitespace. This makes Python code clean and readable, but you must be consistent with your indentation!

Python vs Other Languages - Character Set Comparison

How does Python's character set compare to other popular programming languages? Here's a quick comparison:

Feature 🐍 Python β˜• Java ⚑ C πŸ“œ JavaScript
Unicode Support βœ… Full βœ… Full ⚠️ Limited βœ… Full
Whitespace Significance βœ… Yes ❌ No ❌ No ❌ No
Case Sensitivity βœ… Yes βœ… Yes βœ… Yes βœ… Yes
Special Characters Rich set Rich set Limited Rich set

Character Encoding in Python

Encoding is how characters are stored in memory. Python 3 uses Unicode by default, which means it can handle any character you throw at it.

UTF-8 is the most common encoding used with Python. It's efficient and supports all Unicode characters. When you read a file or get input from a user, Python handles the encoding for you automatically.

# Python automatically handles Unicode
greeting = "Hello, δΈ–η•Œ!"  # Mixed English and Chinese
print(greeting)            # Output: Hello, δΈ–η•Œ!

# You can also specify encoding when reading files
with open('file.txt', 'r', encoding='utf-8') as f:
    content = f.read()

Common Mistakes with Python Characters

Here are the most common mistakes beginners make with Python's character set:

❌ Mixing Tabs and Spaces

Python requires consistent indentation. If you use tabs in one line and spaces in another, Python will get confused and raise an IndentationError.

βœ… The Fix: Use 4 spaces for indentation (PEP 8 recommendation). Most code editors can convert tabs to spaces automatically.

❌ Using Invalid Characters in Variable Names

Variable names can only contain letters, digits, and underscores. They must start with a letter or underscore. Characters like @, #, $ are not allowed.

βœ… The Fix: Use my_var instead of my@var or my-var.

❌ Forgetting Quotes Around Strings

Strings must be enclosed in quotes ('...' or "..."). Without quotes, Python thinks it's a variable name.

βœ… The Fix: Use name = "John" instead of name = John.

❌ Case Sensitivity Mistakes

Python is case-sensitive. myVariable and MyVariable are different names.

βœ… The Fix: Be consistent with your naming conventions. Use snake_case for variables and functions (PEP 8 recommendation).

πŸ“ Quick Reference - Python Character Set

Here's everything you need to remember about Python's character set:

βœ… Allowed Characters

Letters: A-Z, a-z
Digits: 0-9
Special: _ (underscore) only in identifiers
Whitespace: space, tab, newline
Operators: + - * / = == != < > etc.
Punctuation: ( ) [ ] { } : ; . ,

❌ Not Allowed in Identifiers

# Not allowed in variable names
@, #, $, %, ^, &, *
-, +, =, /, \
!, ?, <, >, .
Spaces, tabs (except as separators)
Digits at the start
Keywords (if, else, for, etc.)

πŸ“ Rules for Identifiers

1. Start with letter or underscore
2. Followed by letters, digits, or underscores
3. Case-sensitive (VAR != var)
4. Cannot be a Python keyword
5. Length is unlimited

βœ… Valid: my_var, _private, var1
❌ Invalid: 1var, my-var, my@var

πŸ”€ Unicode Support

# Python supports Unicode characters
name = "JosΓ©"           # Accented characters
greeting = "δ½ ε₯½"        # Chinese
emoji = "πŸš€"           # Emojis work too!

# Check if a character is allowed
print("Ξ±".isalpha())   # True
print("1".isdigit())   # True

Try It Yourself - Practice with Characters

Let's practice with Python characters! Try the examples below, then modify them to experiment:

Loading Pyodide... 0%
Python Code Editor
=== Character Examples ===
my_name: Python
MyName: Python
_name_: underscore
var123: 100
Emoji: 🐍
Chinese: δ½ ε₯½
=== Character Properties ===
Is 'a' a letter? True
Is '5' a digit? True
Is '_' a valid identifier start? True

🎯 Challenge Yourself!
β€’ Try using your name as a variable (with special characters if you have any)
β€’ Test which characters are allowed in identifiers using .isidentifier()
β€’ Try writing a sentence in your native language using Unicode characters

πŸ†

πŸŽ‰ You've Mastered Python Characters!

You now understand Python's character set β€” the foundation of everything you'll write in Python. You know about letters, digits, special symbols, and whitespace. You understand encoding and can avoid common mistakes. That's a solid foundation!

Quick Quiz - Test Your Knowledge

Let's check your understanding of Python's character set:

1. Which of the following is a valid Python variable name?
2. What is the default character encoding in Python 3?
3. Which of the following is NOT a valid Python character category?

Frequently Asked Questions

πŸ€” Can I use spaces in variable names? β–Ό

No, spaces are not allowed in variable names. Python uses spaces as separators between tokens. Use underscores (_) instead, like my_variable. This is actually the recommended style in Python (PEP 8).

πŸ“ Does Python support emojis in code? β–Ό

Yes! Python 3 supports emojis and other Unicode characters. You can use them in strings, comments, and even variable names (though that's not recommended for readability). For example: πŸš€ = "rocket" is valid but not good practice.

πŸ”€ What's the difference between ASCII and Unicode? β–Ό

ASCII is a 7-bit encoding that represents 128 characters (English letters, digits, and basic symbols). Unicode is a much larger standard that represents over 143,000 characters from all writing systems. Python 3 uses Unicode by default, which means you can work with any language or symbol.

⚠️ Why did I get an "IndentationError" in Python? β–Ό

Indentation errors occur when Python's indentation rules are violated. This usually happens when:
  • You mix tabs and spaces
  • Your indentation is inconsistent (e.g., 2 spaces in one line, 4 in another)
  • You forget to indent code inside a block (if, for, etc.)
The fix is to use consistent indentation β€” 4 spaces is the recommended standard.

πŸ“š Where to Go From Here

You've mastered the character set! Here are the next topics to explore:

πŸ”£ Python Tokens

Learn about keywords, identifiers, and literals

πŸ“Š Data Types

Explore numbers, strings, and booleans in Python

πŸ“ Variables & Identifiers

Learn how to name and use variables effectively

πŸ“– Related Tutorials
  • What is Python?
  • Tokens in Python
  • Data Types in Python
  • Variable And Identifiers

πŸ“š Explore More Topics

πŸ—„οΈ SQL Interview Questions

SELECT Restricting Subqueries DML Window Functions

🐍 Python Interview Questions

Intro Syntax Data Types If-Else Functions OOP

β˜• Java Interview Questions

Intro Data Types Strings OOP Collections
Previous: What is Python Next: Tokens in Python β†’