Saturday, February 15, 2020

Changing String Case in Python

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

  1. str.lower()- Return a copy of the string with all the cased characters converted to lowercase.
  2. str.upper()- Return a copy of the string with all the cased characters converted to uppercase.
  3. str.capitalize()- Return a copy of the string with its first character capitalized and the rest lowercased.
  4. 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.

  1. str.islower()- Return true if all cased characters in the string are lowercase and there is at least one cased character, false otherwise.
  2. str.isupper()- Return true if all cased characters in the string are uppercase and there is at least one cased character, false otherwise.
  3. 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

  1. Getting Substring in Python String
  2. String Slicing in Python
  3. String Length in Python - len() Function
  4. Convert String to float in Python
  5. Python Program to Check Armstrong Number

You may also like-

  1. Python continue Statement With Examples
  2. Encapsulation in Python
  3. Class And Object in Python
  4. List in Python With Examples
  5. Just In Time Compiler (JIT) in Java
  6. Bucket Sort Program in Java
  7. Spring MVC Exception Handling Tutorial
  8. What is Hadoop Distributed File System (HDFS)