Case Conversion Methods in Python

Case Conversion Methods in Python (Syntax + Examples)

Python provides several built-in string methods to convert text between different cases (uppercase, lowercase, title case, etc.). Below are the key methods with syntax and examples:


1. upper() – Convert to Uppercase

Purpose: Converts all characters in a string to uppercase.
Syntax:

python

string.upper()

Examples:

python

text = "Hello, World!"
print(text.upper())  # Output: "HELLO, WORLD!"

filename = "data.txt"
print(filename.upper())  # Output: "DATA.TXT"

2. lower() – Convert to Lowercase

Purpose: Converts all characters in a string to lowercase.
Syntax:

python

string.lower()

Examples:

python

text = "PyThOn Is FuN"
print(text.lower())  # Output: "python is fun"

email = "USER@EXAMPLE.COM"
print(email.lower())  # Output: "user@example.com"

3. capitalize() – Capitalize First Letter

Purpose: Converts the first character to uppercase and the rest to lowercase.
Syntax:

python

string.capitalize()

Examples:

python

text = "hello world"
print(text.capitalize())  # Output: "Hello world"

name = "aLiCe"
print(name.capitalize())  # Output: "Alice"

4. title() – Convert to Title Case

Purpose: Capitalizes the first letter of each word (like a title).
Syntax:

python

string.title()

Examples:

python

text = "python programming is fun"
print(text.title())  # Output: "Python Programming Is Fun"

book = "the great gatsby"
print(book.title())  # Output: "The Great Gatsby"

5. swapcase() – Swap Cases

Purpose: Swaps uppercase letters to lowercase and vice versa.
Syntax:

python

string.swapcase()

Examples:

python

text = "Hello, WoRLD!"
print(text.swapcase())  # Output: "hELLO, WOrld!"

code = "PyThOn"
print(code.swapcase())  # Output: "pYtHoN"

6. casefold() – Aggressive Lowercase (Unicode Support)

Purpose: Similar to lower(), but more aggressive (handles special Unicode characters).
Syntax:

python

string.casefold()

Examples:

python

text = "Straße"  # German for "street"
print(text.casefold())  # Output: "strasse" (lowercase + special handling)

word = "İstanbul"
print(word.casefold())  # Output: "i̇stanbul" (better for case-insensitive comparisons)

Summary Table

MethodPurposeExample
upper()Convert to uppercase"hello".upper() → "HELLO"
lower()Convert to lowercase"WORLD".lower() → "world"
capitalize()Capitalize first letter"python".capitalize() → "Python"
title()Convert to title case"hello world".title() → "Hello World"
swapcase()Swap uppercase/lowercase"PyThOn".swapcase() → "pYtHoN"
casefold()Strong lowercase (Unicode)"Straße".casefold() → "strasse"

These methods are useful for text normalization, comparisons, and formatting. Let me know if you need more details! 🚀

Similar Posts

  • Built-in Object & Attribute Functions in python

    1. type() Description: Returns the type of an object. python # 1. Basic types print(type(5)) # <class ‘int’> print(type(3.14)) # <class ‘float’> print(type(“hello”)) # <class ‘str’> print(type(True)) # <class ‘bool’> # 2. Collection types print(type([1, 2, 3])) # <class ‘list’> print(type((1, 2, 3))) # <class ‘tuple’> print(type({1, 2, 3})) # <class ‘set’> print(type({“a”: 1})) # <class…

  • Global And Local Variables

    Global Variables In Python, a global variable is a variable that is accessible throughout the entire program. It is defined outside of any function or class. This means its scope is the entire file, and any function can access and modify its value. You can use the global keyword inside a function to modify a…

  • Predefined Character Classes

    Predefined Character Classes Pattern Description Equivalent . Matches any character except newline \d Matches any digit [0-9] \D Matches any non-digit [^0-9] \w Matches any word character [a-zA-Z0-9_] \W Matches any non-word character [^a-zA-Z0-9_] \s Matches any whitespace character [ \t\n\r\f\v] \S Matches any non-whitespace character [^ \t\n\r\f\v] 1. Literal Character a Matches: The exact character…

  • Linear vs. Scalar,Homogeneous vs. Heterogeneous 

    Linear vs. Scalar Data Types in Python In programming, data types can be categorized based on how they store and organize data. Two important classifications are scalar (atomic) types and linear (compound) types. 1. Scalar (Atomic) Data Types 2. Linear (Compound/Sequential) Data Types Key Differences Between Scalar and Linear Data Types Feature Scalar (Atomic) Linear (Compound) Stores Single…

  • Bank Account Class with Minimum Balance

    Challenge Summary: Bank Account Class with Minimum Balance Objective: Create a BankAccount class that automatically assigns account numbers and enforces a minimum balance rule. 1. Custom Exception Class python class MinimumBalanceError(Exception): “””Custom exception for minimum balance violation””” pass 2. BankAccount Class Requirements Properties: Methods: __init__(self, name, initial_balance) deposit(self, amount) withdraw(self, amount) show_details(self) 3. Key Rules: 4. Testing…

  • The print() Function in Python

    The print() Function in Python: Complete Guide The print() function is Python’s built-in function for outputting data to the standard output (usually the console). Let’s explore all its arguments and capabilities in detail. Basic Syntax python print(*objects, sep=’ ‘, end=’\n’, file=sys.stdout, flush=False) Arguments Explained 1. *objects (Positional Arguments) The values to print. You can pass multiple items separated by commas. Examples:…

Leave a Reply

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