Thursday, February 4, 2021

Case conversion in Python - Upper case, Lower case and Capitalization

Case conversion in Python

While working with strings Case conversions become essential for different reasons. Specially when we need to compare two different strings. 

There are different methods in Python to do case conversion or to check whether the string is in upper case or lower case etc. 

Let's have a look at these methods in detail.

Convert a string to upper case

Converting a string to upper case can be done by using method upper(). There is another useful method isupper() to check if a string is already in upper case or not. 

upper() method returns a string in upper case. Original string will not be changed to upper case by using this method.

Syntax: str.upper()

isupper() method returns True if a string is in upper case and False if not. 

Syntax: str.isupper()

Let's have a look at a simple example with the use of both upper() and isupper(). 

Convert a string to upper case in Python

In the above example, 
  • Line - 4: isupper() is used in if condition. If the string is in upper case it returns True. In this case, string is not in upper case so returns False. 
  • Line - 6: upper() is used in print statement. This method converts the string and returns the same and is printed by print statement. 

Converting a string to lower case

Similar to converting a string to upper case, string can be converted to lower case using the method lower() and islower() can be used to check if a string is in lower case.

lower() method returns a string in lower case. Original string will not be changed to lower case by using this method.

Syntax: str.lower()

islower() method returns True if a string is in lower case and False if not. 

Syntax: str.islower()

Let's have a look at a simple example with the use of both lower() and islower().

Converting a string to lower case in Python

In the above example, 
  • Line - 4: islower() is used in if condition. If the string is in lower case it returns True. In this case, string is not in lower case so returns False. 
  • Line - 6: lower() is used in print statement. This method converts the string and returns the same and is printed by print statement. 
Other than converting the string to upper case or lower case, there are few more conversions that can be done on Python. 
  • Capitalizing a string. 
  • Making a string as title.
  • Reversing (swap) the case of a string.
  • Case fold for case less comparison.

Capitalizing a string

What is capitalizing a string and what is the difference between converting a string to upper case and capitalizing? 

Capitalizing a string would mean, only the first letter of a string would be converted to upper case (CAPITAL) and convert the remaining letters to lower case (SMALL) irrespective of the current case of the letter. 

capitalize() method is used to capitalize a string. This method accepts no arguments and returns the capitalized form of a string. This doesn't update the original string.

Syntax:

str.capitalize()

Capitalizing a string in Python

In the above example, 
  • string_one holds a string with mixed case letters. First letter of the string would be converted to upper case and the remaining letters to lower case. 
  • string_two holds a string with all upper case letters. First letter of the string would be converted to upper case (i.e., will stay as upper case) and remaining letters would be converted to lower case.
  • string_three holds a string with all lower case letters. First letter of the string would be converted to upper case and remaining letters would be converted to lower case (i.e., will stay as lower case).

Making a string as Title

String capitalization would only make the first letter of a string as capital (or upper case) and convert the remaining letters to small (or lower case). But, Every word in a title starts with capital letter and the remaining letters in that word would be small letters. 

This can be done by using title() method. This method accepts no arguments and returns the string in Title format. 

Syntax:

str.title()

There is one important point to note here is any special character, number or blank space would be considered as separator and next letter would be converted to capital. 

This can be easily understood with below example. 

Convert a string as  Title in Python

In the above example, we are using five different scenarios. 
  • string_one holds a string with mixed case letters each word is separated by spaces. Every letter after a space would be considered as new word and first letter would be converted to upper case and the following letters to lower case. 
  • string_two and string_three hold all upper case and lower case letters accordingly with each word separated by space. Conversion of a string would be similar to string_one
  • string_four holds all lower case letters with each letter is separated by a space. In this scenario Python would consider each letter as a separate word and converts them to upper case. Result would be all upper case letters separated by a space.
  • string_five holds all upper case letters separated by a space and a number '1'. Even though there is no space in 'A1BC', Python would consider '1' as separator and leave both A and B in upper case and converts 'C' to lower case. 
Case conversion in Python

Reverse the case of a string

Reversing the case would mean converting every letter into the opposite case.
  • Upper case letter to lower case letter.
  • Lower case letter to upper case letter.
Any special characters would stay as is. 

This can be done by using the method swapcase(). This method doesn't accept any arguments and returns a string with the case reversed for each letter. This doesn't update the original string. 

Syntax:

str.swapcase()

This is a straight forward example. And, personally I haven't used this much. 

Swap case of a string in Python

Case fold for case less comparison

Case less comparison is very useful when comparing two different strings. This helps compare the two string in different cases without affecting the original strings. 

casefold() method can be used to do this. Primary purpose of this is for comparison. This method doesn't accept any arguments and returns the string in lower case. 

Syntax:

str.casefold()

Below is a simple example to show how the data is returned and comparison is done. 

case fold in Python

In the above example, 
  • string_one and string_two holds the strings with mixed case letters. casefold() returns the strings converted to lower case (all letters). 
  • Line - 7: comparing these two string by using casefold() method would convert both the string to lower case and compare. In this example, it returns True. 
Using upper() and lower() methods also would produce the same result in terms of comparison. 

Hope the above was a bit of help to understand the case conversion in Python. 


If you have any Suggestions or Feedback, Please leave a comment below or use Contact Form.

2 comments:

  1. It should be noted that casefold() is lower() that also converts Unicode like to Latin:

    >>> "Scheiße." == "Scheisse."
    False
    >>> "Scheiße.".lower() == "Scheisse.".lower()
    False
    >>> "Scheiße.".casefold() == "Scheisse.".casefold()
    True

    ReplyDelete

Different Ways of Sorting Data in a List - Python

Sorting Data in a List List is a collection of data (of different data types), much like an array. Like any data structure or data set, dat...