[Self-study Python] Python string case conversion

Python string case conversion

outline

insert image description here

Python string uppercase to lowercase

In the development process, we often need to convert all uppercase characters of a string to lowercase. In Python , the function used to convert uppercase characters of a string to lowercase is lower().

Detailed explanation of Python lower() function

grammar

S.lower() -> str

parameter

parameter describe
s Represents the original string.

return value

The string after converting all to lowercase.

the case

convert string to lowercase

Use the lower() function to convert a string to lowercase

print("嗨客网(www.haicoder.net)")

# 使用 lower() 函数,将字符串转成小写
strHaicoder = "Study Python From HaiCoder"
print("strHaicoder =", strHaicoder.lower())

After the program runs, the console output is as follows:

insert image description here

First, we define a variable strHaicoder of string type, then we use the lower() function of the string to convert all the characters of the variable strHaicoder into lowercase, and use the print() function to print the final converted result.

Strings are originally all lowercase

Use the lower() function to convert a string to lowercase

print("嗨客网(www.haicoder.net)")

# 使用 lower() 函数,将字符串转成小写
strHaicoder = "study python from haicoder"
print("strHaicoder =", strHaicoder.lower())

After the program runs, the console output is as follows:
insert image description here

First, we define a variable strHaicoder of string type, then we use the lower() function of the string to convert all the characters of the variable strHaicoder into lowercase, and use the print() function to print the final converted result.

Because all the characters of the string strHaicoder are all lowercase, so there is no conversion here.

Convert a string containing Chinese

Use the lower() function to convert a string containing Chinese

print("嗨客网(www.haicoder.net)")

# 使用 lower() 函数,转换包含中文的字符串
strHaicoder = "嗨客网(HaiCoder)"
print("strHaicoder =", strHaicoder.lower())

After the program runs, the console output is as follows:

insert image description here

First, we define a variable strHaicoder of string type and assign it as "HaiCoder". Then we use the lower() function of the string to convert the variable strHaicoder into lowercase, and use the print() function to print the final The transformed result.

Because the string strHaicoder contains Chinese, the Chinese has not been changed in any way, and all English uppercase letters in it have been converted to lowercase, so the final output is "海客网(haicoder)".

Python string uppercase to lowercase summary

In Python, the function used to convert uppercase characters of a string to lowercase is lower(). Python lower() function syntax:

S.lower() -> str

Python string lowercase to uppercase

In the development process, we often need to convert all the lowercase characters of a string to uppercase. In Python , the function used to convert the lowercase characters of a string to uppercase is upper().

Detailed explanation of Python upper() function

grammar

S.upper() -> str

parameter

parameter describe
s Represents the original string.

return value

The string after converting all to uppercase.

the case

convert string to uppercase

Use the upper() function to convert a string to uppercase

print("嗨客网(www.haicoder.net)")

# 使用 upper() 函数,将字符串转成大写
strHaicoder = "Study Python From HaiCoder"
print("strHaicoder =", strHaicoder.upper())

After the program runs, the console output is as follows:

insert image description here

First, we define a variable strHaicoder of string type, then we use the upper() function of the string to convert all the characters of the variable strHaicoder into uppercase, and use the print() function to print the final converted result.

Strings are originally all caps

Use the upper() function to convert a string to uppercase

print("嗨客网(www.haicoder.net)")

# 使用 upper() 函数,将字符串转成大写
strHaicoder = "STUDY PYTHON FROM HAICODER"
print("strHaicoder =", strHaicoder.upper())

After the program runs, the console output is as follows:

insert image description here

First, we define a variable strHaicoder of string type, then we use the upper() function of the string to convert all the characters of the variable strHaicoder into uppercase, and use the print() function to print the final converted result.

Because all the characters of the string strHaicoder are originally all uppercase, so no conversion is done here.

Convert a string containing Chinese

Use the upper() function to convert a string containing Chinese

print("嗨客网(www.haicoder.net)")

# 使用 upper() 函数,转换包含中文的字符串
strHaicoder = "嗨客网(HaiCoder)"
print("strHaicoder =", strHaicoder.upper())

After the program runs, the console output is as follows:

insert image description here

First, we define a variable strHaicoder of string type and assign it as "HaiCoder". Then we use the upper() function of the string to convert the variable strHaicoder into uppercase, and use the print() function to print the final The transformed result.

Because the string strHaicoder contains Chinese, the Chinese has not been changed in any way, and all the English lowercase letters in it have been converted to uppercase, so the final output is "HAICODER".

Python string lowercase to uppercase summary

In Python, the function used to convert lowercase characters of a string to uppercase is upper(). Python upper() function syntax:

S.upper() -> str
```xxxxxxxxxx S.upper() -> strpython

Guess you like

Origin blog.csdn.net/weixin_41384860/article/details/128898600