Python Tutorial: How To Use The Python Isalnum String Method

preview_player
Показать описание
Python Isalnum String Method
In this Python tutorial we take a look at the Python isalnum string method in Python. The isalnum string method allow us to check if all the contents of a string are numbers. This string method only works with integers not floating point numbers because floating point numbers contain decimals. If the string object contains all numbers and no other characters we will be returned a boolean of True. If the string object contains anything other than an integer we be returned a boolean of False.

Python Isalnum String Method Syntax
'String'.isalnum()

'String' - The string must contain only numbers(integers) for a True return and if the string object contains anything other a number then we will get a False statement.
Examples Of The Python Isalnum String Method
Example 1:
'1243'.isalnum()
True

Example 2:
'4.5'.isalnum()
False

Example 3:
a = '56'
True

Example 4:
b = ' 98'
False
Examples Explained

Example 1:

'1243'.isalnum() - We create a string object that contains numbers and call the isalnum string method on our string object.

True- We are returned the boolean of True which means all characters contained in the string object are numbers.

Example 2:

'4.5'.isalnum() - We create a string object that contains numbers but this string object also contains a decimal point and then we call the isalnum string method on the string object.

False- We are returned False because our string object contains a decimal.

Example 3:

a = '56'- Here we create a new string object and assign a variable of 'a' to represent our string object. Our string object contains all numbers.

True- We are returned True because our string object contains all numbers.

Example 4:

b = ' 98'- In this example, we create a string object that contains a space and some numbers. We chose the variable 'b' to represent the string object.

False - We are returned False because our string object contains a space.
Conclusion

In this Python tutorial we took a look at the Python isalnum string method. If you have any questions about this string method leave a comment below.
Рекомендации по теме