深度阅读

How to convert a string to lowercase or uppercase in Python

作者
作者
2023年08月22日
更新时间
46.61 分钟
阅读时间
0
阅读量

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:

  1. 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
  1. 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
  1. 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.

相关标签

博客作者

热爱技术,乐于分享,持续学习。专注于Web开发、系统架构设计和人工智能领域。