Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

Python3 isalnum() Method

Python3 isalnum() Method

Python3 Strings Python3 Strings


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.

Syntax

Syntax of the isalnum() method:


str.isalnum()

Parameters

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

print

(

str

.

isalnum

(

)

)

str

=

"

www.runoob.com

"

print

(

str

.

isalnum

(

)

)

The output of the above example is:


True
False

More examples:

Example

Example 1: a string containing letters and digits

print

(

"abc123"

.

isalnum

(

)

)

 

Output: True

Example 2: a string containing spaces

print

(

"abc 123"

.

isalnum

(

)

)

 

Output: False

Example 3: a string containing special characters

print

(

"abc#123"

.

isalnum

(

)

)

 

Output: False

Example 4: an empty string

print

(

""

.

isalnum

(

)

)

 

Output: False

Example 5: a string containing only digits

print

(

"123456"

.

isalnum

(

)

)

 

Output: True

Example 6: a string containing only letters

print

(

"abcdef"

.

isalnum

(

)

)

 

Output: True

Example 7: a string containing an underscore but no letters or digits

print

(

"abc_def"

.

isalnum

(

)

)

 

Output: False


Python3 Strings Python3 Strings