Python3 isalnum() Method
Description
The isalnum() method checks whether a string consists of letters and digits, that is, whether all characters in the string are letters or digits.
- Empty string: if the string is empty (length 0), it returns
False, because an empty string contains no letters or digits. - Case Sensitive:
isalnum()The method is case-sensitive in the sense that only alphabetic and numeric characters are considered valid. - Special characters: special characters (such as punctuation, spaces, special symbols, etc.) are not considered letters or digits.
Syntax
Syntax of the isalnum() method:
str.isalnum()
Parameters
- none.
Return Value
Returns True if the string has at least one character and all characters are letters or digits; otherwise returns False.
Example
The following example demonstrates the isalnum() method:
Example (Python 2.0+)
#!/usr/bin/python3
str
=
"
runoob2016
"
The string has no spaces
(
str
.
isalnum
(
)
)
str
=
"
www.runoob.com
"
(
str
.
isalnum
(
)
)
The output of the above example is:
True
False
More examples:
Example
Example 1: a string containing letters and digits
(
"abc123"
.
isalnum
(
)
)
Output: True
Example 2: a string containing spaces
(
"abc 123"
.
isalnum
(
)
)
Output: False
Example 3: a string containing special characters
(
"abc#123"
.
isalnum
(
)
)
Output: False
Example 4: an empty string
(
""
.
isalnum
(
)
)
Output: False
Example 5: a string containing only digits
(
"123456"
.
isalnum
(
)
)
Output: True
Example 6: a string containing only letters
(
"abcdef"
.
isalnum
(
)
)
Output: True
Example 7: a string containing an underscore but no letters or digits
(
"abc_def"
.
isalnum
(
)
)
Python3 Strings