1. ใช้ isdigit()
>> "12345".isdigit() True >>> "12345a".isdigit() False
2. ใช้ Regex ^[0-9]+$
>> import re
>>> bool(re.match('^[0-9]+$', '123abc'))
False
>>> bool(re.match('^[0-9]+$', '123'))
True
re.match() คืน match object ต้องแปลงเป็น bool()
1. ใช้ isdigit()
>> "12345".isdigit() True >>> "12345a".isdigit() False
2. ใช้ Regex ^[0-9]+$
>> import re
>>> bool(re.match('^[0-9]+$', '123abc'))
False
>>> bool(re.match('^[0-9]+$', '123'))
True
re.match() คืน match object ต้องแปลงเป็น bool()