Skip to main content

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.

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

Post a Comment

Popular posts from this blog

All about READ in RPGLE & Why we use it with SETLL/SETGT?

READ READ is one of the most used Opcodes in RPGLE. As the name suggests main purpose of this Opcode is to read a record from Database file. What are the different READ Opcodes? To list, Below are the five Opcodes.  READ - Read a Record READC - Read Next Changed Record READE - Read Equal Key Record READP - Read Prior Record READPE - Read Prior Equal Record We will see more about each of these later in this article. Before that, We will see a bit about SETLL/SETGT .  SETLL (Set Lower Limit) SETLL accepts Key Fields or Relative Record Number (RRN) as Search Arguments and positions the file at the Corresponding Record (or Next Record if exact match isn't found).  SETGT (Set Greater Than) SETGT accepts Key Fields or Relative Record Number (RRN) as Search Arguments and positions the file at the Next Record (Greater Than the Key value). Syntax: SETLL SEARCH-ARGUMENTS/KEYFIELDS FILENAME SETGT  SEARCH-ARGUMENTS/KEYFIELDS FILENAME One of the below can be passed as Search Arguments. Key Fiel

What we need to know about CHAIN (RPGLE) & How is it different from READ?

CHAIN READ & CHAIN, These are one of the most used (& useful) Opcodes by any RPG developer. These Opcodes are used to read a record from file. So, What's the difference between CHAIN & READ?   CHAIN operation retrieves a record based on the Key specified. It's more like Retrieving Random record from a Database file based on the Key fields.  READ operation reads the record currently pointed to from a Database file. There are multiple Opcodes that start with READ and all are used to read a record but with slight difference. We will see more about different Opcodes and How they are different from each other (and CHAIN) in another article. Few differences to note.  CHAIN requires Key fields to read a record where as READ would read the record currently pointed to (SETLL or SETGT are used to point a Record).  If there are multiple records with the same Key data, CHAIN would return the same record every time. READE can be used to read all the records with the specified Ke

Extract a portion of a Date/Time/Timestamp in RPGLE - IBM i

%SUBDT Extracting Year, Month, Day, Hour, Minutes, Seconds or Milli seconds of a given Date/Time/Timestamp is required most of the times.  This can be extracted easily by using %SUBDT. BIF name looks more similar to %SUBST which is used to extract a portion of string by passing from and two positions of the original string. Instead, We would need to pass a value (i.e., Date, Time or Timestamp ) and Unit (i.e., *YEARS, *MONTHS, *DAYS, *HOURS, *MINUTES, *SECONDS or *MSECONDS) to %SUBDT.  Valid unit should be passed for the type of the value passed. Below are the valid values for each type. Date - *DAYS, *MONTHS, *YEARS Time - *HOURS, *MINUTES, *SECONDS Timestamp - *DAYS, *MONTHS, *YEARS, *HOURS, *MINUTES, *SECONDS, *MSECONDS Syntax: %SUBDT(value : unit { : digits { : decpos} }) Value and Unit are the mandatory arguments.  Digits and Decimal positions are optional and can only be used with *SECONDS for Timestamp. We can either pass the full form for the unit or use the short form. Below i