String Functions in Python3 (Part-2)2 min read

We discussed some string functions in ‘Built-in String Functions’.There are some more essential functions which are discussed here.

String Functions in Python3
String Function in Python 3

1. isalpha()

Returns true if string contains only alphabetic letters, false otherwise.
Syntax:- str.isalpha()

Note:- Whitespace characters and special characters are not considered in this group.

Example:-
>>>str="Python"
>>>print(str.isalpha())
True
>>>str="Python!"
>>>print(str.isalpha())
False
>>>#Returns False as string contains exclamation mark(special character)
>>>str="Power of Python" #Here string contains whitespace characters.
>>>print(str.isalpha())
False

2. islower()

Returns true if all characters in the string are lowercase, false otherwise.
Syntax:- str.islower()

Example:-
>>>str="Python"
>>>print(str.islower())
False
>>>str="python!"
>>>print(str.islower())
True 
>>>#Here special characters are allowed.
>>>str="Power of Python" #Here string contains whitespace characters.
>>>print(str.islower())
True #Whitespaces are also allowed

3. isupper()

Returns true if all characters in the string are uppercase,false otherwise.
Syntax:- str.upper()

Example:-
>>>str="WELCOMe"
>>>print(str.isupper())
False
>>>str="PROGRAMMING THROUGH PYTHON!"
>>>print(str.isupper())
True 
>>>#Here special characters are allowed.
#Whitespaces are also allowed

4. istitle()

Returns true if string is properly ‘titled-case’.Returns False if the string is not title cased or empty.
Syntax:- str.istitle()

Example:-
>>>str="Learn Python"
>>>print(str.istitle())
True
>>>str=Bahut scope Hain Engineering Mein
>>>print(str.istitle())
False
>>>str=" "
>>>print(str.istitle())
False

5. isnumeric()

Returns true if all characters of the string are numeric characters. Otherwise, it returns False.
Numeric characters include all integers,fractions,subscript,superscript roman numerals etc.

Note:-It is advised to go through Python documentation once.
Syntax:- str.isnumeric()

Example:-
>>>str="1 2 3 4 5"
>>>print(str.isnumeric())
False 
>>>#It contains whitespace also
>>>str="134653"
>>>print(str.isnumeric())
True

Code to find all numeric characters in unicode

import sys
t=[]
for i in range(sys.maxunicode + 1):
    s = chr(i)
    if s.isnumeric():
        t.append(s)
print(*t,end=" ")

6. isspace()

Returns true if string contains only whitespace characters, false otherwise.
Syntax:- str.isspace()

Example:-
>>str=" "
>>>print(str.isspace())
True
>>>str=" Welcome                 "
>>>print(str.isspace())
False
Bookmark(0)
Tags:

Add a Comment

Your email address will not be published. Required fields are marked *