swapcase(), capitalize() & isdigit() Function in Python

Python has wide range of function for string handling. some functions for string handling are swapcase() function which converts the all the uppercase to lowercase and viceversa. capitalize() function which converts the first character of entire string to the uppercase and the remaining characters will remain in lowercase. isdigit() function which returns True if a string has only numeric digits. if not it will return false.

swapcase(), capitalize() & isdigit() Function in Python:

  • swapcase() function in python converts the string from lowercase to uppercase and vice versa
  • capitalize() function in python converts first character of the string  to uppercase and other characters will be in lowercase
  • isdigit() function in python returns true only if the entire string is digits . if not it will return false

 

swapcase() function in python 

swapcase(), capitalize() & isdigit() function in python 2

swapcase() Function in python swaps the case of the every single character of the string from upper case to lower case and from lower case to upper case.

  • If the input string is in small case it swaps the result to upper case.
  • If the input string is in upper case it swaps the result to small case.
  • If input string is in mix of small case and upper case then it swaps all the upper case character to small case and all the small case character to upper case.

Syntax of swapcase() Function in Python:

str.swapcase();

Example of swapcase() Function in Python:

str1 = "Salute to the Mother Earth"
str2= "Jupiter is the largest planet"
str3= "MERCURY IS CLOSE TO SUN"

str1.swapcase()
str2.swapcase()
str3.swapcase()

swapcase() function simply swaps each and every character in the string so the output will be

sALUTE TO THE mOTHER eARTH

JUPITER IS THE LARGEST PLANET

mercury is close to sun

NOTE:

  1. swapcase() function does not take any arguments, Therefore, It returns an error if a parameter is passed.
  2. Digits and symbols are returned as it is, Only text string is converted to lowercase and uppercase.

 


Capitalize() function in python :

In Python, the capitalize() function converts the first character of a string to capital (uppercase) letter, all the other characters will be in lowercase.

swapcase(), capitalize() & isdigit() function in python 1

Syntax of capitalize() Function in Python:

str.capitalize()

 

Example of capitalize() Function in Python:

str1 = "Salute to the Mother Earth"
str2= "Jupiter is the largest planet"
str3= "MERCURY IS CLOSE TO SUN"

str1.capitalize()
str2.capitalize()
str3.capitalize()

capitalize() function simply capitalizes the first character in the string so the output will be

Salute to the mother earth

Jupiter is the largest planet

Mercury is close to sun

NOTE:

  1. Digits and symbols are returned as it is, Only First character string is converted to uppercase.

 


isdigit() function in python

Syntax for isdigit() Function in Python:

str.isdigit()
1.True- If all characters in the string are digits.
2.False- If the string contains 1 or more non-digit characters.

Example of isdigit() Function in python:

swapcase(), capitalize() & isdigit() function in python 3

str1 = "2018 IS AT DOORSTEP"; 
str1.isdigit()

str1="887161"
str1.isdigit()

so the output will be

False
True

some more examples:  below are some more examples for isdigit() function

>>> ”.isdigit()
False
>>> ‘xyz123’.isdigit()
False
>>> ‘xyz’.isdigit()
False
>>> ‘123’.isdigit()
True
>>> ‘Abc Def’.isdigit()
False
>>> ‘fgh ijk’.isdigit()
False
>>> ‘%$!@#’.isdigit()
False
>>> ‘ABC’.isdigit()
False

For further understanding of swapcase(), capitalize() & isdigit() Function in Python one can refer the documentation

 


Other Related Topics

 

previous swapcase() Function in Python                                                                                                          next swapcase() Function in Python

Author

  • Sridhar Venkatachalam

    With close to 10 years on Experience in data science and machine learning Have extensively worked on programming languages like R, Python (Pandas), SAS, Pyspark.

    View all posts