The isalnum() Function is used to check if all the letters in the String are alphanumeric or not.
i.e. Alphanumeric String actually means, the String should only consist of numbers (0 to 9) or alphabets (A to Z or a to z). Any values other than that will not be considered.
x = "29B" if x.isalnum(): print("All the letters in the String are alphanumeric") else: print("No! Not all the letters in the String are alphanumeric")
In the above code, we have declared a String '29B' that has alphanumeric characters only.
So, to check, if all the letters in the String are alphanumeric, we have used 'isalnum( )' function.
if x.isalnum(): print("All the letters in the String are alphanumeric") else: print("No! Not all the letters in the String are alphanumeric")
And in this case, all the letters in the String are alphanumeric. So, we got the below output.
Similarly, let us look at the next example.
x = "29 B" if x.isalnum(): print("All the letters in the String are alphanumeric") else: print("No! Not all the letters in the String are alphanumeric")
In the above code, we have declared a String '29 B' that has a space other than alphanumeric characters.
So, to check, if all the letters in the String are alphanumeric, we have used 'isalnum( )' function.
if x.isalnum(): print("All the letters in the String are alphanumeric") else: print("No! Not all the letters in the String are alphanumeric")
And in this case, all the letters in the String are not alphanumeric. So, we got the below output.