How to convert a string to lowercase or uppercase in Python
Published on Aug. 22, 2023, 12:15 p.m.
To convert a string to lowercase in Python
To convert a string to lowercase in Python, you can use the lower()
method. Here’s an example:
my_string = "This IS a MixED CaSE STRIng"
lowercase_string = my_string.lower()
print(lowercase_string)
Output:
this is a mixed case string
In this example, we use the lower()
method to convert the string my_string
to lowercase and store the result in a new variable called lowercase_string
.
The lower()
method returns a copy of the original string with all alphabetic characters converted to lowercase. Any characters that are not alphabetic are left unchanged. If the original string is already all lowercase, the lower()
method returns the original string unchanged.
To convert a string to uppercase in Python
To convert a string to uppercase in Python, you can use the upper()
method. Here’s an example:
my_string = "This IS a MixED CaSE STRIng"
uppercase_string = my_string.upper()
print(uppercase_string)
Output:
THIS IS A MIXED CASE STRING
In this example, we use the upper()
method to convert the string my_string
to uppercase and store the result in a new variable called uppercase_string
.
The upper()
method returns a copy of the original string with all alphabetic characters converted to uppercase. Any characters that are not alphabetic are left unchanged. If the original string is already all uppercase, the upper()
method returns the original string unchanged.
To convert the first character of a string to uppercase in Python
To convert the first character of a string to uppercase in Python, you can use the capitalize()
method. Here’s an example:
my_string = "this is a sentence."
capitalized_string = my_string.capitalize()
print(capitalized_string)
Output:
This is a sentence.
In this example, we use the capitalize()
method to convert the first character of the string my_string
to uppercase and store the result in a new variable called capitalized_string
.
The capitalize()
method returns a copy of the original string with the first character converted to uppercase and the remaining characters in lowercase. If the original string is already capitalized or empty, the capitalize()
method returns the original string unchanged.
To convert a string to titlecase (i.e., capitalize the first letter of each word)
To convert a string to titlecase (i.e., capitalize the first letter of each word), you can use the title()
method in Python. Here’s an example:
my_string = "this is a sentence."
title_string = my_string.title()
print(title_string)
Output:
This Is A Sentence.
In this example, we use the title()
method to convert the string my_string
to titlecase and store the result in a new variable called title_string
.
The title()
method returns a copy of the original string with the first character of each word converted to uppercase and the remaining characters in lowercase. If the original string is already in titlecase or empty, the title()
method returns the original string unchanged.
There are many other operations that you can perform on strings in Python
There are many other operations that you can perform on strings in Python. Here are a few examples:
- Concatenation - you can concatenate two or more strings using the
+
operator:
string1 = "Hello"
string2 = "World"
combined_string = string1 + " " + string2
print(combined_string)
Output:
Hello World
- Slicing - you can slice a string to get a substring using the
[start:end]
notation:
my_string = "This is a sentence."
substring = my_string[0:4]
print(substring)
Output:
This
- Formatting - you can format a string using the
format()
method or f-strings:
name = "Alice"
age = 25
message = "My name is {} and I am {} years old.".format(name, age)
print(message)
Or using f-strings
message = f”My name is {name} and I am {age} years old.”
print(message)
Output:
My name is Alice and I am 25 years old.
My name is Alice and I am 25 years old.
These are just a few examples of operations that you can perform on strings in Python. There are many more, and the Python documentation has a comprehensive list of string methods and operations.