A Beginner Guide For Python String Method

In This Article You Will Learn About Python String Method isdigit(), isidentifier(), islower(), isnumeric() and isprintable()

Python String Method – Before moving ahead, let’s know a little bit about methods index(), isalnum(), isdecimal()

isdigit() method – It returns True if all the characters are digits, otherwise False.

Syntax - string.isdigit()

Parameter Values - No parameters.

Note: It considered exponent powers as digit.

Example 1- Check if all the characters in the given string are digits.

x = '56427'
y = x.isdigit()

print(y)
python-string-method
As it is shown clearly that variable contains only digit character therefore, it returned True.

Example 2- Check if all the characters in the given string are digits.

x = '6829yeuk'
y = x.isdigit()

print(y)
python-string-method
As it is shown clearly that variable contains both digit and alphabet character therefore, it returned False.

Digit type – It contains two types of digit which includes numeric type = digit and numeric type = decimal.

Note: – Isdigit() method gives False if we use fraction and roman numerals.

isidentifier() method – It returns True if the string is a valid identifier, otherwise False.

Valid identifier – A string is considered as a valid identifier if it contains only letter (a-z), numbers (0-9) and underscore(_). A valid identifier cannot start with a number, or contain any spaces.

Syntax - string.isidentifier()

Parameter Values - No parameters.

Example 1- Check if the string is valid identifier.

x = 'Hello,Python'
z = x.isidentifier()

print(z)
python-string-method
As it is shown clearly that string contains punctuation therefore it returned False.

Example 2- Check if the string is valid identifier.

x = 'Hello,Python'
z = 'code no. 65'
y = 'Hello Python'
v = '5five'
b = ' Python'
c = 'hello'
n = 'five5'

print(x.isidentifier())
print(z.isidentifier())
print(y.isidentifier())
print(v.isidentifier())
print(b.isidentifier())
print(c.isidentifier())
print(n.isidentifier())
python-string-method
As it is shown clearly that it returned True and False according to string.

islower() method – It returns True if all the characters are in lower case, otherwise False.

Syntax - string.lower()

Parameter Values - No parameters value.

Example 1- Check if all characters of string is in lower case.

x = 'hello, python'
z = 'HELLO, PYTHON'

print(x.islower())
print(z.islower())
python-string-method
As it is shown clearly that first variable is in lower case and second is in upper case therefore, it returned True and False respectively. 

Note: – islower() method doesn’t convert numbers, symbols and space into lower case.

Example 2- Check if all characters of string is in lower case.

x = '1234'
z = '&#'

print(x.islower())
print(z.islower())
python-string-method
As it is shown clearly that first variable contains number and second symbol therefore, it returned False and False respectively. 

isnumeric() method – It returns True if all the characters are numeric (0-9), otherwise False. It also considered exponent values as numeric value.

Syntax - string.isnumeric()

Parameter Values - No parameter values.

Example 1- Check if all the characters in the string are numeric.

x = '1234'
z = 'card no., 6528'

print(x.isnumeric())
print(z.isnumeric())
python-string-method
As it is shown clearly that variable name x is numeric and variable name z is alpha-numeric, therefore, it executed True and False respectively. 

isprintable() method – It returns True if all the characters are printable, otherwise False. In other words string can be combination of alphabet (a-z), number (0-9), symbol ($,@,#), whitespace, punctuation and underscore (_).   

Syntax - string.isprintable()

Parameter Values - No parameters.

Example 1- Check if all the characters in the string are printable.

x = 'Hello, Python world!'
z = "'Hello!\n, you"

print(x.isprintable())
print(z.isprintable())
python-string-method
As it is shown variable name z is executed False because it is not printable as it forward slash (/).

If you find anything incorrect in above discussed topic and have any further question, please comment down below.

Like us on

1 thought on “A Beginner Guide For Python String Method”

Leave a Comment

Stay in the loop

codingstreets