Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> Python

วิธีเช็คว่า String เป็น Alphanumeric ใน Python

1. ใช้ isalnum()

>> '123abc'.isalnum()
True
>>> '123#$%abc'.isalnum()
False

2. ใช้ Regex ^[a-zA-Z0-9]+$

>> import re
>>> bool(re.match('^[a-zA-Z0-9]+$', '123abc'))
True
>>> bool(re.match('^[a-zA-Z0-9]+$', '123#$%abc'))
False