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

  • 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…

  • Positional-Only Arguments in Python

    Positional-Only Arguments in Python Positional-only arguments are function parameters that must be passed by position (order) and cannot be passed by keyword name. Syntax Use the / symbol in the function definition to indicate that all parameters before it are positional-only: python def function_name(param1, param2, /, param3, param4): # function body Simple Examples Example 1: Basic Positional-Only Arguments python def calculate_area(length,…

  • 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…

  • re.split()

    Python re.split() Method Explained The re.split() method splits a string by the occurrences of a pattern. It’s like the built-in str.split() but much more powerful because you can use regex patterns. Syntax python re.split(pattern, string, maxsplit=0, flags=0) Example 1: Splitting by Multiple Delimiters python import retext1=”The re.split() method splits a string by the occurrences of a pattern. It’s like…

  • 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…

  • Examples of Python Exceptions

    Comprehensive Examples of Python Exceptions Here are examples of common Python exceptions with simple programs: 1. SyntaxError 2. IndentationError 3. NameError 4. TypeError 5. ValueError 6. IndexError 7. KeyError 8. ZeroDivisionError 9. FileNotFoundError 10. PermissionError 11. ImportError 12. AttributeError 13. RuntimeError 14. RecursionError 15. KeyboardInterrupt 16. MemoryError 17. OverflowError 18. StopIteration 19. AssertionError 20. UnboundLocalError…

Leave a Reply

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