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

  • What is list

    In Python, a list is a built-in data structure that represents an ordered, mutable (changeable), and heterogeneous (can contain different data types) collection of elements. Lists are one of the most commonly used data structures in Python due to their flexibility and dynamic nature. Definition of a List in Python: Example: python my_list = [1, “hello”, 3.14,…

  • re module

    The re module is Python’s built-in module for regular expressions (regex). It provides functions and methods to work with strings using pattern matching, allowing you to search, extract, replace, and split text based on complex patterns. Key Functions in the re Module 1. Searching and Matching python import re text = “The quick brown fox jumps over the lazy dog” # re.search()…

  • Strings in Python Indexing,Traversal

    Strings in Python and Indexing Strings in Python are sequences of characters enclosed in single quotes (‘ ‘), double quotes (” “), or triple quotes (”’ ”’ or “”” “””). They are immutable sequences of Unicode code points used to represent text. String Characteristics Creating Strings python single_quoted = ‘Hello’ double_quoted = “World” triple_quoted = ”’This is…

  • Functions as Parameters in Python

    Functions as Parameters in Python In Python, functions are first-class objects, which means they can be: Basic Concept When we pass a function as a parameter, we’re essentially allowing one function to use another function’s behavior. Simple Examples Example 1: Basic Function as Parameter python def greet(name): return f”Hello, {name}!” def farewell(name): return f”Goodbye, {name}!” def…

  • What are Variables

    A program is essentially a set of instructions that tells a computer what to do. Just like a recipe guides a chef, a program guides a computer to perform specific tasks—whether it’s calculating numbers, playing a song, displaying a website, or running a game. Programs are written in programming languages like Python, Java, or C++,…

Leave a Reply

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