The Python String class has an isalnum () method that can be called on a string and tells us if the string consists of alphanumeric characters only or not. You can call it like this:
print ('123abc'.isalnum ())
print ('123 # $% abc'.isalnum ())
You can also use regular expressions for the same result. To match literal numbers, we can call re.match (regex, string) using the regular expression: "^ [a-zA-Z0-9] + $". For example,
import re print (bool (re.match ('^ [a-zA-Z0-9] + $', '123abc')))
import re print (bool (re.match ('^ [a-zA-Z0-9] + $ ',' 123abc # $% ')))
re.match returns an object, to check if it exists or not, we need to convert it to boolean using bool ().
Acquire and analyze data from all corners of the social web with Python. This book is for intermediate Python developers who want to engage with the use of public APIs to collect data from social m...
R for Everyone: Advanced Analytics and Graphics. ...
A recipe for having fun and getting things done with the Raspberry Pi ...
This book is not just about learning the code; even if you learn to program. If you want to program professionally, learning to code is not enough; For this reason, in addition to helping you program,...