Get the App
SLTechnology News&Howtos  ›  Development  › 

Case conversion method of Python string letters

Shulou Source: shulou.com Published: 2022-05-31 16:59:07 10月01日 Update

This article mainly introduces the "Python string letter case conversion method". In the daily operation, I believe that many people have doubts about the Python string letter case conversion method. The editor consulted all kinds of data and sorted out a simple and easy operation method. I hope it will be helpful to answer the doubts of "Python string letter case conversion method". Next, please follow the editor to study!

1.lower ()

Convert uppercase letters in a string to lowercase letters

Syntax: str.lower ()

# example: str1 = "LiBai is a boy" str2 = "WWW.jrcg.com" print (strl) print ("after conversion:", str1.lower ()) print (str2) print ("after conversion:", str2.lower ())

Running result:

LiBai is a boy

After conversion: libai is a boy

WWW.jrcg.com

After conversion: www.jrcg.com

2.upper ()

Convert lowercase letters of a string to uppercase letters

Syntax: str.upper ()

Str1 = "www.libai.com" print (str1) print ("after conversion:", str1.upper ())

Running result:

Www.libai.com

After conversion: WWW.LIBAI.COM

3.capitalize ()

Make the first letter of the string uppercase and the rest lowercase.

Syntax: str.capitalize ()

Strl = "happy new year" print (strl.capitalize ())

Running result:

Happy new year

4.title ()

Returns a string that satisfies the title format, that is, the first letters of all English words are capitalized and the rest of the letters are lowercase.

Syntax: str.title ()

Str3 = "i love Python" print (str3.title ())

Running result:

I Love Python

5.swapcase ()

Interchange the uppercase and lowercase letters in the string str at the same time, that is, convert the uppercase letters in the string str to lowercase letters and convert lowercase letters to uppercase letters.

Syntax: str.swapcase ()

Str3 = "jingRUOchENguang" print (str3.swapcase)

Running result:

Running result: JINGruoCHenGUANG

6.casefold ()

Converts all uppercase letters in a string to lowercase letters. You can also convert uppercase to lowercase in non-English languages.

The difference between the # lower () function and the casefold () function: the lower () method is only encoded for ASCII, that is, it works for Amurz, and it is not valid for other languages to convert uppercase to lowercase. You can only use the casefold () function.

At this point, the study on the "Python string letter case conversion method" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

Tags: Letters characters strings uppercase lowercase methods results run size uppercase letters grammar learning functions English more language help practical effective next Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Huawei Shulou Technology Microsoft Redmi Xiaomi