Python Tutorial

Python Variable

Python Operators

Python Sequence

Python String

Python Flow Control

Python Functions

Python Class and Object

Python Class Members (properties and methods)

Python Exception Handling

Python Modules

Python File Operations (I/O)

Python string case conversion

In Python, strings have built-in methods to convert their case. In this tutorial, we'll cover the following methods: lower(), upper(), capitalize(), title(), swapcase() and casefold().

1. lower()

The lower() method returns a new string with all characters converted to lowercase.

text = "Hello, World!"
lowercased = text.lower()
print(lowercased)  # Output: hello, world!

2. upper()

The upper() method returns a new string with all characters converted to uppercase.

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

3. capitalize()

The capitalize() method returns a new string with the first character capitalized and the rest of the characters converted to lowercase.

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

4. title()

The title() method returns a new string with the first character of each word capitalized and the rest of the characters in each word converted to lowercase.

text = "hello, world! welcome to python."
titled = text.title()
print(titled)  # Output: Hello, World! Welcome To Python.

5. swapcase()

The swapcase() method returns a new string with the case of each character swapped. Uppercase characters are converted to lowercase, and lowercase characters are converted to uppercase.

text = "Hello, World!"
swapped = text.swapcase()
print(swapped)  # Output: hELLO, wORLD!

6. casefold()

The casefold() method returns a new string with all characters converted to their casefolded version, which is a more aggressive form of lowercasing that is suitable for case-insensitive comparisons. It is especially useful when comparing strings in languages with more complex character sets.

text = "Straße"
casefolded = text.casefold()
print(casefolded)  # Output: strasse

Example

Here's an example of using case conversion methods for case-insensitive string comparisons:

user_input = "YeS"
normalized_input = user_input.lower()

if normalized_input == "yes":
    print("User agreed.")
else:
    print("User disagreed.")

In this example, we use the lower() method to normalize the user input, so our comparison works regardless of the input's case.

  1. Uppercase and Lowercase String Conversion in Python:

    • Use upper() to convert to uppercase and lower() to convert to lowercase.
    # Example
    text = "Hello, World!"
    upper_text = text.upper()
    lower_text = text.lower()
    
  2. Changing Case of Strings with Python:

    • Adjust the case of strings using various methods.
    # Example
    text = "Mixed Case String"
    upper_text = text.upper()
    lower_text = text.lower()
    title_text = text.title()
    
  3. String to Lowercase in Python:

    • Use the lower() method to convert a string to lowercase.
    # Example
    text = "Lowercase String"
    lower_text = text.lower()
    
  4. Converting String to Uppercase in Python:

    • Use the upper() method to convert a string to uppercase.
    # Example
    text = "Uppercase String"
    upper_text = text.upper()
    
  5. Title Case Conversion in Python:

    • Use the title() method to convert the first character of each word to uppercase.
    # Example
    text = "title case conversion"
    title_text = text.title()
    
  6. Python capitalize() Method:

    • The capitalize() method capitalizes the first character of a string.
    # Example
    text = "capitalize first letter"
    capitalized_text = text.capitalize()
    
  7. Case-Insensitive String Comparison in Python:

    • Use case-insensitive comparison using casefold() or lower().
    # Example
    text1 = "Hello"
    text2 = "hello"
    is_equal = text1.casefold() == text2.casefold()
    
  8. Python String Case Conversion Without Using Built-in Methods:

    • Manually convert case without using built-in methods.
    # Example
    text = "Custom Case Conversion"
    custom_lower = ''.join(c.lower() for c in text)
    custom_upper = ''.join(c.upper() for c in text)
    
  9. Handling Mixed Case Strings in Python:

    • Use a combination of methods to handle strings with mixed cases.
    # Example
    mixed_case_text = "miXed CaSe StRinG"
    normalized_text = mixed_case_text.lower().capitalize()