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() 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:
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:
- swapcase() function does not take any arguments, Therefore, It returns an error if a parameter is passed.
- 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.
Syntax of capitalize() Function in Python:
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:
- 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:
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:
str1 = "2018 IS AT DOORSTEP"; str1.isdigit() str1="887161" str1.isdigit()
so the output will be
some more examples: below are some more examples for isdigit() function
False
False
False
True
False
False
False
False
For further understanding of swapcase(), capitalize() & isdigit() Function in Python one can refer the documentation
Other Related Topics
- ljust(),rjust() and center() function in python
- String max() & min() function in python
- lstrip(),rstrip() and strip() function in python
- lower() upper() & title() function in python
- string split in python
- splitlines() Function in Python
- zfill() Function in Python
- startswith() Function in Python
- Extract first n characters from left of column in pandas python
- Extract last n characters from right of the column in pandas python
- Replace a substring of a column in pandas python