Python string Method - isalnum()
The isalnum()
method is a built-in Python string method that returns a boolean value indicating whether all the characters in the string are alphanumeric (i.e., either letters or digits).
Here's an example that demonstrates the usage of the isalnum()
method:
string1 = "Hello123" string2 = "Hello, world!" print(string1.isalnum()) # Output: True print(string2.isalnum()) # Output: False
In this example, we create two strings: string1
which contains alphanumeric characters only, and string2
which contains a non-alphanumeric character. We then call the isalnum()
method on each string. The method returns True
for string1
since all its characters are alphanumeric, and False
for string2
since it contains a non-alphanumeric character.
The isalnum()
method returns True
if the string is not empty and all its characters are alphanumeric, and False
otherwise. Here are some additional examples:
print("".isalnum()) # Output: False print("123".isalnum()) # Output: True print("Hello, world!".isalnum()) # Output: False
In the first example, we call the isalnum()
method on an empty string, which returns False
since the string is not alphanumeric. In the second example, we call the method on a string containing only digits, which returns True
. In the third example, we call the method on a string that contains non-alphanumeric characters, which returns False
.