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)
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.
Uppercase and Lowercase String Conversion in Python:
upper()
to convert to uppercase and lower()
to convert to lowercase.# Example text = "Hello, World!" upper_text = text.upper() lower_text = text.lower()
Changing Case of Strings with Python:
# Example text = "Mixed Case String" upper_text = text.upper() lower_text = text.lower() title_text = text.title()
String to Lowercase in Python:
lower()
method to convert a string to lowercase.# Example text = "Lowercase String" lower_text = text.lower()
Converting String to Uppercase in Python:
upper()
method to convert a string to uppercase.# Example text = "Uppercase String" upper_text = text.upper()
Title Case Conversion in Python:
title()
method to convert the first character of each word to uppercase.# Example text = "title case conversion" title_text = text.title()
Python capitalize()
Method:
capitalize()
method capitalizes the first character of a string.# Example text = "capitalize first letter" capitalized_text = text.capitalize()
Case-Insensitive String Comparison in Python:
casefold()
or lower()
.# Example text1 = "Hello" text2 = "hello" is_equal = text1.casefold() == text2.casefold()
Python String Case Conversion 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)
Handling Mixed Case Strings in Python:
# Example mixed_case_text = "miXed CaSe StRinG" normalized_text = mixed_case_text.lower().capitalize()