Python Tutorial (33) – Example: String case conversion

Time: Column:Python views:280

Python Code Demonstrating String Case Conversion

The following Python code demonstrates how to convert a string to uppercase, lowercase, and how to format it using capitalization methods like capitalize() and title().

Example: String Case Conversion

# Define the string
str = "www.pmeve.com"

# Convert all characters to uppercase
print(str.upper())          # Converts all lowercase letters to uppercase

# Convert all characters to lowercase
print(str.lower())          # Converts all uppercase letters to lowercase

# Capitalize the first letter of the string, and make the rest lowercase
print(str.capitalize())     # Capitalizes the first letter of the string

# Convert the first letter of each word to uppercase
print(str.title())          # Capitalizes the first letter of each word

Sample Output:

WWW.PMEVE.COM
www.pmeve.com
Www.pmeve.com
Www.Pmeve.Com

Explanation:

  • upper(): Converts all lowercase letters in the string to uppercase.

  • lower(): Converts all uppercase letters in the string to lowercase.

  • capitalize(): Converts only the first letter of the string to uppercase and the rest to lowercase.

  • title(): Converts the first letter of each word in the string to uppercase and the remaining letters to lowercase.