In the previous article, we have discussed Python Program to Calculate EMI
isalnum() Method:
The isalnum() Method is a String Class pre-defined method that can be used on any String Object. It returns Boolean Values based on the following criteria.
- If all of the characters in the given string are Alphanumerical, it returns True.
- If any of the characters in the given string are not Alphanumerical, this function returns False.
For Example:
Given string = “Hellobtechgeeks123”
Output: returns true since all the characters are alphabets and numbers.
Given string = “good morning@ #btechgeeks12345”
Output: returns False since all the given String contains some special symbols like #,@, and spaces.
Examples:
Example1:
Input:
Given string = "Hellobtechgeeks123"
Output:
The Given string { Hellobtechgeeks123 } is Alpha Numeric
Example 2:
Input:
Given string = "good morning@ #btechgeeks12345"
Output:
The Given string { good morning@ #btechgeeks12345 } is not Alpha Numeric
Program to Check if all Characters of String are Alphanumeric or Not
Below are the ways to Check if all Characters of String are Alphanumeric or Not.
Method #1: Using isalnum() Method (Static input)
Approach:
- Give the string as static input and store it in a variable.
- Check whether the Given string is alphanumeric using the built-in alnum() method and store it in another variable.
- Check whether the above result is True or not using the if conditional statement.
- If it is True, Print the given string is alphanumeric.
- If it is False, Print the given string that is not alphanumeric.
- The Exit of the program.
Below is the implementation:
# Give the string as static input and store it in a variable. gvn_strng = "Hellobtechgeeks123" # Check whether the Given string is alphanumeric using built-in alnum() method # and store it in another variable. Bol_val = gvn_strng.isalnum() # Check whether the above result is True or not using the if conditional statement. if(Bol_val == True): # If it is True, Print the given string is alphanumeric. print("The Given string {", gvn_strng, "} is Alpha Numeric") else: # If it is False, Print the given string is not alphanumeric. print("The Given string {", gvn_strng, "} is not Alpha Numeric")
Output:
The Given string { Hellobtechgeeks123 } is Alpha Numeric
Method #2: Using isalnum() Method (User input)
Approach:
- Give the string as user input using the input() function and store it in a variable.
- Check whether the Given string is alphanumeric using the built-in alnum() method and store it in another variable.
- Check whether the above result is True or not using the if conditional statement.
- If it is True, Print the given string is alphanumeric.
- If it is False, Print the given string that is not alphanumeric.
- The Exit of the program.
Below is the implementation:
# Give the string as user input using the input()function and store it in a variable. gvn_strng = input('Enter some random string = ') # Check whether the Given string is alphanumeric using built-in alnum() method # and store it in another variable. Bol_val = gvn_strng.isalnum() # Check whether the above result is True or not using the if conditional statement. if(Bol_val == True): # If it is True, Print the given string is alphanumeric. print("The Given string {", gvn_strng, "} is Alpha Numeric") else: # If it is False, Print the given string is not alphanumeric. print("The Given string {", gvn_strng, "} is not Alpha Numeric")
Output:
Enter some random string = good morning @btechgeeks #123456 The Given string { good morning @btechgeeks #123456 } is not Alpha Numeric
Explore more instances related to python concepts from Python Programming Examples Guide and get promoted from beginner to professional programmer level in Python Programming Language.