In this tutorial we’ll go through all the methods which are used for changing string case in Python.
In Python there are various methods for changing case of String and also for checking if String is lower case, upper case etc.
Summary of the methods for changing String case in Python
- str.lower()- Return a copy of the string with all the cased characters converted to lowercase.
- str.upper()- Return a copy of the string with all the cased characters converted to uppercase.
- str.capitalize()- Return a copy of the string with its first character capitalized and the rest lowercased.
- str.title()- Return a titlecased version of the string where words start with an uppercase character and the remaining characters are lowercase.
There there are also methods to check the String case, these methods can be used to verify if changing case is really required or not.
- str.islower()- Return true if all cased characters in the string are lowercase and there is at least one cased character, false otherwise.
- str.isupper()- Return true if all cased characters in the string are uppercase and there is at least one cased character, false otherwise.
- str.istitle()- Return true if the string is a titlecased string and there is at least one character.
Changing String case in Python examples
1. Changing String to all lower case or to all upper case.
s = "This is a TEST String" print('String in all lower case-',s.lower()) s = "This is a Test String" print('String in all upper case-', s.upper())
Output
String in all lower case- this is a test string String in all upper case- THIS IS A TEST STRING
2. Capitalizing the String. First character will be capitalized, if there is any other upper case character in the String that is lower cased.
s = "this is a TEST String" print('String Capitalized-',s.capitalize())
Output
String Capitalized- This is a test string
3. String title cased. Using title() method you can title cased a String. This method capitalizes the first character of every word.
s = "this is a TEST String" print('String Title cased-',s.title())
Output
String Title cased- This Is A Test String
4. Checking the case before changing. You can also check the case before changing the case of the String and change the case only if needed. This is an optimization because any String modification method results in a creation of a new String as String is immutable in Python.
s = "this is a test string" #change only if not already in lower case if not s.islower(): s = s.lower() else: print('String already in lower case') print(s)
Output
String already in lower case this is a test string
s = "This is a test String" #change only if not already in upper case if not s.isupper(): s = s.upper() else: print('String already in upper case') print(s)
Output
THIS IS A TEST STRING
That's all for this topic Changing String Case in Python. If you have any doubt or any suggestions to make please drop a comment. Thanks!
>>>Return to Python Tutorial Page
Related Topics
You may also like-