Python Case-Sensitivity Rules

🏷️ Python Basics: Syntax, Variables, and Types / Basic Syntax Rules

Python is a case-sensitive programming language, which means it treats uppercase and lowercase letters as completely different characters. This is one of the first rules new engineers encounter when writing Python code, and understanding it helps avoid frustrating errors.


βš™οΈ What Does Case-Sensitivity Mean?

Case-sensitivity means that Python distinguishes between identifiers based on their letter casing. For example, the variable name myVar is different from myvar, MyVar, or MYVAR. Each of these would be treated as separate, independent variables in your program.


πŸ•΅οΈ Key Rules to Remember

  • Variable names are case-sensitive: username and UserName are two different variables
  • Function names follow the same rule: calculate_total() is not the same as Calculate_Total()
  • Keywords in Python are always lowercase: True, False, and None are the only exceptions (they start with capital letters)
  • Built-in functions like print(), len(), and type() must be written in lowercase
  • Module names are typically lowercase, but once imported, you must use the exact casing

πŸ“Š Case-Sensitivity Comparison Table

Expression Valid? Explanation
myVar = 10 βœ… Standard lowercase variable
myvar = 20 βœ… Different variable from myVar
MyVar = 30 βœ… Third distinct variable
print("Hello") βœ… Correct casing for built-in function
Print("Hello") ❌ Python will raise a NameError
True βœ… Python keyword with capital T
true ❌ Not recognized as a keyword
len("test") βœ… Correct lowercase function name
Len("test") ❌ Python will raise a NameError

πŸ› οΈ Common Mistakes and How to Avoid Them

  • Inconsistent variable naming: If you define a variable as userName but later try to access it as username, Python will throw a NameError
  • Mixing up built-in functions: Writing Print() instead of print() is one of the most common beginner mistakes
  • Confusing keywords: Remember that None, True, and False are the only keywords with capital letters
  • Import errors: When importing modules, the casing must match exactly. import OS will fail if the module is named os

πŸ’‘ Best Practices for Engineers

  • Choose a naming convention and stick with it consistently throughout your project
  • Use lowercase with underscores for variable names (snake_case) like user_name or total_count
  • Use CamelCase for class names like UserProfile or DatabaseConnection
  • Always double-check the casing of built-in functions and keywords
  • Use an IDE or code editor with syntax highlighting to catch casing errors early

πŸ§ͺ Quick Self-Check

Ask yourself these questions when debugging case-sensitivity issues:

  • Did I define the variable with the exact same casing I'm using to access it?
  • Am I using print() (lowercase) or Print() (capital P)?
  • Is the keyword True written with a capital T?
  • Does the module name match the casing in my import statement?

πŸ“ Final Thought

Case-sensitivity is a fundamental rule in Python that becomes second nature with practice. The key is consistencyβ€”choose a naming style, stick with it, and always verify your casing when encountering unexpected errors. Remember, Python treats data, Data, and DATA as three completely different things, so precision matters.


Python treats uppercase and lowercase letters as different characters β€” Name, name, and NAME are three separate identifiers.


πŸ”€ Example 1: Variable names are case-sensitive

This shows that two variables with the same letters but different case are completely different.

name = "Alice"
Name = "Bob"
print(name)
print(Name)

πŸ“€ Output: Alice
πŸ“€ Output: Bob


πŸ”€ Example 2: Function names are case-sensitive

This shows that calling a function with wrong case causes an error.

def greet():
    return "Hello"

print(greet())
print(Greet())

πŸ“€ Output: Hello
πŸ“€ Output: NameError: name 'Greet' is not defined


πŸ”€ Example 3: Keywords are always lowercase

This shows that Python keywords like True, False, and None must be written exactly as shown.

is_active = True
print(is_active)
print(TRUE)

πŸ“€ Output: True
πŸ“€ Output: NameError: name 'TRUE' is not defined


πŸ”€ Example 4: String values are case-sensitive

This shows that Python treats the same word in different case as different strings.

status = "active"
if status == "Active":
    print("Match found")
else:
    print("No match")

πŸ“€ Output: No match


πŸ”€ Example 5: Class and method names are case-sensitive

This shows that class names and their methods must be called with exact case.

class Car:
    def start(self):
        return "Engine running"

my_car = Car()
print(my_car.start())
print(my_car.Start())

πŸ“€ Output: Engine running
πŸ“€ Output: AttributeError: 'Car' object has no attribute 'Start'


πŸ“Š Comparison Table: Case-Sensitivity Rules

Element Type Correct Case Example Incorrect Case Example Behavior
Variable user_id User_Id Creates a new variable
Function calculate() Calculate() Raises NameError
Keyword True TRUE Raises NameError
String value "active" "Active" Different string, no match
Method .start() .Start() Raises AttributeError

Python is a case-sensitive programming language, which means it treats uppercase and lowercase letters as completely different characters. This is one of the first rules new engineers encounter when writing Python code, and understanding it helps avoid frustrating errors.


βš™οΈ What Does Case-Sensitivity Mean?

Case-sensitivity means that Python distinguishes between identifiers based on their letter casing. For example, the variable name myVar is different from myvar, MyVar, or MYVAR. Each of these would be treated as separate, independent variables in your program.


πŸ•΅οΈ Key Rules to Remember

  • Variable names are case-sensitive: username and UserName are two different variables
  • Function names follow the same rule: calculate_total() is not the same as Calculate_Total()
  • Keywords in Python are always lowercase: True, False, and None are the only exceptions (they start with capital letters)
  • Built-in functions like print(), len(), and type() must be written in lowercase
  • Module names are typically lowercase, but once imported, you must use the exact casing

πŸ“Š Case-Sensitivity Comparison Table

Expression Valid? Explanation
myVar = 10 βœ… Standard lowercase variable
myvar = 20 βœ… Different variable from myVar
MyVar = 30 βœ… Third distinct variable
print("Hello") βœ… Correct casing for built-in function
Print("Hello") ❌ Python will raise a NameError
True βœ… Python keyword with capital T
true ❌ Not recognized as a keyword
len("test") βœ… Correct lowercase function name
Len("test") ❌ Python will raise a NameError

πŸ› οΈ Common Mistakes and How to Avoid Them

  • Inconsistent variable naming: If you define a variable as userName but later try to access it as username, Python will throw a NameError
  • Mixing up built-in functions: Writing Print() instead of print() is one of the most common beginner mistakes
  • Confusing keywords: Remember that None, True, and False are the only keywords with capital letters
  • Import errors: When importing modules, the casing must match exactly. import OS will fail if the module is named os

πŸ’‘ Best Practices for Engineers

  • Choose a naming convention and stick with it consistently throughout your project
  • Use lowercase with underscores for variable names (snake_case) like user_name or total_count
  • Use CamelCase for class names like UserProfile or DatabaseConnection
  • Always double-check the casing of built-in functions and keywords
  • Use an IDE or code editor with syntax highlighting to catch casing errors early

πŸ§ͺ Quick Self-Check

Ask yourself these questions when debugging case-sensitivity issues:

  • Did I define the variable with the exact same casing I'm using to access it?
  • Am I using print() (lowercase) or Print() (capital P)?
  • Is the keyword True written with a capital T?
  • Does the module name match the casing in my import statement?

πŸ“ Final Thought

Case-sensitivity is a fundamental rule in Python that becomes second nature with practice. The key is consistencyβ€”choose a naming style, stick with it, and always verify your casing when encountering unexpected errors. Remember, Python treats data, Data, and DATA as three completely different things, so precision matters.

Interactive Views

You are currently in πŸ“š All-in-One mode. Use the tabs at the top to switch to πŸ“– Theory Only or πŸ’» Code Only views.

Python treats uppercase and lowercase letters as different characters β€” Name, name, and NAME are three separate identifiers.


πŸ”€ Example 1: Variable names are case-sensitive

This shows that two variables with the same letters but different case are completely different.

name = "Alice"
Name = "Bob"
print(name)
print(Name)

πŸ“€ Output: Alice
πŸ“€ Output: Bob


πŸ”€ Example 2: Function names are case-sensitive

This shows that calling a function with wrong case causes an error.

def greet():
    return "Hello"

print(greet())
print(Greet())

πŸ“€ Output: Hello
πŸ“€ Output: NameError: name 'Greet' is not defined


πŸ”€ Example 3: Keywords are always lowercase

This shows that Python keywords like True, False, and None must be written exactly as shown.

is_active = True
print(is_active)
print(TRUE)

πŸ“€ Output: True
πŸ“€ Output: NameError: name 'TRUE' is not defined


πŸ”€ Example 4: String values are case-sensitive

This shows that Python treats the same word in different case as different strings.

status = "active"
if status == "Active":
    print("Match found")
else:
    print("No match")

πŸ“€ Output: No match


πŸ”€ Example 5: Class and method names are case-sensitive

This shows that class names and their methods must be called with exact case.

class Car:
    def start(self):
        return "Engine running"

my_car = Car()
print(my_car.start())
print(my_car.Start())

πŸ“€ Output: Engine running
πŸ“€ Output: AttributeError: 'Car' object has no attribute 'Start'


πŸ“Š Comparison Table: Case-Sensitivity Rules

Element Type Correct Case Example Incorrect Case Example Behavior
Variable user_id User_Id Creates a new variable
Function calculate() Calculate() Raises NameError
Keyword True TRUE Raises NameError
String value "active" "Active" Different string, no match
Method .start() .Start() Raises AttributeError