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

Python Check Whether a String Is Alphanumeric

Python Check Whether a String Is Alphanumeric

Document Object Reference Manual Python3 Examples

In Python, we can use the string's isalnum() method to determine whether a string contains only letters and digits. If all characters in the string are letters or digits, it returns True; otherwise, returns False。

Example

def

is_alphanumeric

(

s

)

:

   

return

s.

isalnum

(

)

Testing

test_string1

=

"Hello123"

test_string2

=

"Hello 123"

test_string3

=

"Hello@123"

print

(

is_alphanumeric

(

test_string1

)

)

 

Output True

print

(

is_alphanumeric

(

test_string2

)

)

 

Output False

print

(

is_alphanumeric

(

test_string3

)

)

 

Output False

Code analysis:

Output result:

True
False
False

Document Object Reference Manual Python3 Examples