Program for Python String isalnum() Method

In the previous article, we have discussed Python Program for center() Function
isalnum() Function in Python:

The isalnum() method returns a value True if all of the characters are alphanumeric, which means they are alphabet letters (a-z) and numbers (0-9).

(space)!#%&? etc are examples of characters that are not alphanumeric.

Syntax:

string.isalnum()

Parameters: This function has no parameters.

Examples:

Example1:

Input:

Given first string = "pythonprograms"
Given second string = "10345 @@@good morning"

Output:

The above given first string is : pythonprograms
The given first string after applying isalnum() function: True
The above given second string is : 10345 @@@good morning
The given second string after applying isalnum() function: False

Example2:

Input:

Given first string = "hello this is python programming platform"
Given second string = "hello123"

Output:

The above given first string is : hello this is python programming platform
The given first string after applying isalnum() function: False
The above given second string is : hello123
The given second string after applying isalnum() function: True

Program for isalnum() Function in Python

Method #1: Using Built-in Functions (Static Input)

Approach:

  • Give the first string as static input and store it in a variable.
  • Apply isalnum() function to the given first string that returns a value True if all of the characters are alphanumeric, which means they are alphabet letters (a-z) and numbers (0-9).
  • Store it in another variable.
  • Print the above-given String.
  • Print the above-given String after applying isalnum() function.
  • Similarly, do the same for other string and print the result string.
  • The Exit of Program.

Below is the implementation:

# Give the first string as static input and store it in a variable.
gvn_fststr = "pythonprograms"
# Apply isalnum() function to the given first string that returns a value
# True if all of the characters are alphanumeric, which means they are alphabet
# letters (a-z) and numbers (0-9).
# Store it in another variable.
rslt_str1 = gvn_fststr.isalnum()
# Print the above given String
print("The above given first string is :", gvn_fststr)
# Print the above given String after applying isalnum() function.
print("The given first string after applying isalnum() function:", rslt_str1)
# Similarly do the same for other string and print the result string.
gvn_scndstr = "10345 @@@good morning"
rslt_str2 = gvn_scndstr.isalnum()
print("The above given second string is :", gvn_scndstr)
print("The given second string after applying isalnum() function:", rslt_str2)

Output:

The above given first string is : pythonprograms
The given first string after applying isalnum() function: True
The above given second string is : 10345 @@@good morning
The given second string after applying isalnum() function: False

Method #2: Using Built-in Functions (User Input)

Approach:

  • Give the first string as user input using the input() function and store it in a variable.
  • Apply isalnum() function to the given first string that returns a value True if all of the characters are alphanumeric, which means they are alphabet letters (a-z) and numbers (0-9).
  • Store it in another variable.
  • Print the above-given String.
  • Print the above-given String after applying isalnum() function.
  • Similarly, do the same for other string and print the result string.
  • The Exit of Program.

Below is the implementation:

# Give the first string as user input using the input() function and store it in a variable.
gvn_fststr = input("Enter some Random String = ")
# Apply isalnum() function to the given first string that returns a value
# True if all of the characters are alphanumeric, which means they are alphabet
# letters (a-z) and numbers (0-9).
# Store it in another variable.
rslt_str1 = gvn_fststr.isalnum()
# Print the above given String
print("The above given first string is :", gvn_fststr)
# Print the above given String after applying isalnum() function.
print("The given first string after applying isalnum() function:", rslt_str1)
# Similarly do the same for other string and print the result string.
gvn_scndstr = input("Enter some Random String = ")
rslt_str2 = gvn_scndstr.isalnum()
print("The above given second string is :", gvn_scndstr)
print("The given second string after applying isalnum() function:", rslt_str2)

Output:

Enter some Random String = hello this is python programming platform
The above given first string is : hello this is python programming platform
The given first string after applying isalnum() function: False
Enter some Random String = hello123
The above given second string is : hello123
The given second string after applying isalnum() function: True

Go through our tutorial and learn about various Python String Method Examples and learn how to apply the knowledge while dealing with strings.