Skip to content
  •  
  • Uncategorized

[Python] How To Convert String Case

Last Updated on 2021-12-11 by Clay

Using Python to process the case conversion of strings, or the comparison of upper and lower case strings is an extraordinarily easy task. First, Python is a simple and easy-to-write programming language. Second, these commonly used capitalization conversion methods have long been built into Python's string processing.

Today, I will record the following different Python string case processing:

  • Determine case
  • Convert case

Determine case

  • islower(): Determine whether the string is all lowercase
  • isupper(): Determine whether the string is all uppercase
  • istitle(): Determine whether the string conforms to the title format (the first letter is capitalized)
a = "lower"
b = "UPPER"
c = "This Is A Title"

print(a.islower())
print(b.isupper())
print(c.istitle())


Output:

True
True
True



Convert case

  • lower(): Convert all characters in the string to lowercase letters
  • upper(): Convert all characters in the string to uppercase letters
  • capitalize(): Convert the first letter to uppercase and the others to lowercase
  • title(): Convert the first letter of each word to uppercase, the others are lowercase
s = "there is a BIG news!"

print("lower():", s.lower())
print("upper():", s.upper())
print("capitalize():", s.capitalize())
print("title():", s.title())


Output:

lower(): there is a big news!
upper(): THERE IS A BIG NEWS!
capitalize(): There is a big news!
title(): There Is A Big News!



References


Read More

Tags:

Leave a ReplyCancel reply

Exit mobile version