Open In App

Python String isalnum() Method

Last Updated : 27 Feb, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

Python String isalnum() method checks whether all the characters in a given string are either alphabet or numeric (alphanumeric) characters.

Python String isalnum() Method Syntax:

Syntax:  string_name.isalnum() 

Parameter:  isalnum() method takes no parameters 

Return: 

  • True: If all the characters are alphanumeric 
  • False: If one or more characters are not alphanumeric 

Time Complexity: O(1)

Auxiliary Space: O(1)

Python String isalnum() Method Example:

Python program to demonstrate the use of String isalnum() method

Python




# here a,b and c are characters and 1,2 and 3
# are numbers
string = "abc123"
print(string.isalnum())


Output: 

True

Example 1: More examples on Python String isalnum() Method

Python3




string = "abc 123"
print(string, "is alphanumeric?", string.isalnum())
 
string = "abc_123"
print(string, "is alphanumeric?", string.isalnum())
 
string = "000"
print(string, "is alphanumeric?", string.isalnum())
 
string = "aaaa"
print(string, "is alphanumeric?", string.isalnum())


Output:

abc 123 is alphanumeric? False
abc_123 is alphanumeric? False
000 is alphanumeric? True
aaaa is alphanumeric? True

Example 2: isalnum() in if…else Statement

We can also use Python String isalnum() Method along with if…else statements, to output custom messages

Python3




password = "user123456"
 
if password.isalnum():
    print("Password is alphanumeric.")
else:
    print("Password is not alphanumeric.")


Output:

Password is alphanumeric.


Previous Article
Next Article

Similar Reads

Python String Methods | Set 2 (len, count, center, ljust, rjust, isalpha, isalnum, isspace & join)
Some of the string methods are covered in the set 3 below String Methods Part- 1 More methods are discussed in this article 1. len() :- This function returns the length of the string. 2. count("string", beg, end) :- This function counts the occurrence of mentioned substring in whole string. This function takes 3 arguments, substring, beginning posi
4 min read
Class Method vs Static Method vs Instance Method in Python
Three important types of methods in Python are class methods, static methods, and instance methods. Each serves a distinct purpose and contributes to the overall flexibility and functionality of object-oriented programming in Python. In this article, we will see the difference between class method, static method, and instance method with the help o
5 min read
String slicing in Python to check if a string can become empty by recursive deletion
Given a string “str” and another string “sub_str”. We are allowed to delete “sub_str” from “str” any number of times. It is also given that the “sub_str” appears only once at a time. The task is to find if “str” can become empty by removing “sub_str” again and again. Examples: Input : str = "GEEGEEKSKS", sub_str = "GEEKS" Output : Yes Explanation :
2 min read
String slicing in Python to Rotate a String
Given a string of size n, write functions to perform following operations on string. Left (Or anticlockwise) rotate the given string by d elements (where d <= n).Right (Or clockwise) rotate the given string by d elements (where d <= n).Examples: Input : s = "GeeksforGeeks" d = 2Output : Left Rotation : "eksforGeeksGe" Right Rotation : "ksGeek
3 min read
Python | Check if given string can be formed by concatenating string elements of list
Given a string 'str' and a list of string elements, write a Python program to check whether given string can be formed by concatenating the string elements of list or not. Examples: Input : str = 'python' lst = ['co', 'de', 'py', 'ks', 'on'] Output : False Input : str = 'geeks' lst = ['for', 'ge', 'abc', 'ks', 'e', 'xyz'] Output : True Approach #1
5 min read
Python | Check if string ends with any string in given list
While working with strings, their prefixes and suffix play an important role in making any decision. For data manipulation tasks, we may need to sometimes, check if a string ends with any of the matching strings. Let's discuss certain ways in which this task can be performed. Method #1 : Using filter() + endswith() The combination of the above func
6 min read
Python | Sorting string using order defined by another string
Given two strings (of lowercase letters), a pattern and a string. The task is to sort string according to the order defined by pattern and return the reverse of it. It may be assumed that pattern has all characters of the string and all characters in pattern appear only once. Examples: Input : pat = "asbcklfdmegnot", str = "eksge" Output : str = "g
2 min read
Python | Merge Tuple String List values to String
Sometimes, while working with records, we can have a problem in which any element of record can be of type string but mistakenly processed as list of characters. This can be a problem while working with a lot of data. Let's discuss certain ways in which this problem can be solved. Method #1: Using list comprehension + join() The combination of abov
6 min read
Python | Sort each String in String list
Sometimes, while working with Python, we can have a problem in which we need to perform the sort operation in all the Strings that are present in a list. This problem can occur in general programming and web development. Let's discuss certain ways in which this problem can be solved. Method #1 : Using list comprehension + sorted() + join() This is
4 min read
Python | Convert List of String List to String List
Sometimes while working in Python, we can have problems of the interconversion of data. This article talks about the conversion of list of List Strings to joined string list. Let's discuss certain ways in which this task can be performed. Method #1 : Using map() + generator expression + join() + isdigit() This task can be performed using a combinat
6 min read
Article Tags :
Practice Tags :