Working with case of string

There are 4 methods in Python that works with case of a string.

Below are methods that works with case of a string
  1. upper()
  2. lower()
  3. swapcase()
  4. title()
upper() method
This method is use to convert all characters in a string into uppercase or capital letters.
In the below examples all characters in a string is converted into uppercase
>>> str = 'Python is a popular programming language'
>>> str.upper()
'PYTHON IS A POPULAR PROGRAMMING LANGUAGE'
>>> str1 = 'PyThon LAnGuAgE'
>>> str1.upper()
'PYTHON LANGUAGE'
lower() method
This method is use to convert all characters in a string into lower case or small letters.
>>> name = 'roHan jadHaV'
>>> name.lower()
'rohan jadhav'
swapcase() method
This method converts uppercase into lowercase and vice versa.
Below are the examples of swapcase().
>>> city = 'LondoN'
>>> city.swapcase()
'lONDOn'
>>> country = 'UnITed STateS'
>>> country.swapcase()
'uNitED stATEs'

title() method
This method convert first character of each word in a string in uppercase and remaining characters in lowercase. Below are the examples.
>>> a = 'this is the examaple 1'
>>> a.title()
'This Is The Examaple 1'
>>> b = 'this is second example'
>>> b.title()
'This Is Second Example'

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s