Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

python
string
alphanumeric
communitycreator

What is string.isalnum in Python?

Programming Bytes

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

The isalnum method checks if all the characters of a string are alphanumeric. If the string contains only alphanumeric characters, then isalnum returns True; otherwise, it returns False.

Alphanumeric characters include:

  • Alphabets (a-z) (A-Z)
  • Numbers 0-9
  • Subscript, superscript
  • Fractions
  • Roman numerals

Syntax

string.isalnum()

Return value

  • The function returns True if all characters of the string are alphanumerical.

  • The function returns False if even one character of the string is not an alphanumeric character or if the string is empty.

Example 1

string = "123abc"
string.isalnum() #True

In the string above, 123abc, all the characters are alphanumeric, so the function returns True.

Example 2

string = "123 abc"
string.isalnum() #False

In the string above, 123 abc, the space is not an alphanumeric character, so isalnum() returns False.

Example 3

string = ""
string.isalnum() #False

The isalnum method returns False for an empty string.

Example 4

string = "½"
string.isalnum() #True

isalnum returns True for the ½ string because fractions are considered alphanumeric characters.

Code

string = "123abc"
print(string, " isalnum --> ", string.isalnum())
string = "123 abc"
print(string, " isalnum --> ", string.isalnum())
string = ""
print(string, "isalnum -->", string.isalnum())
string = "½"
print(string, "isalnum -->", string.isalnum())

RELATED TAGS

python
string
alphanumeric
communitycreator

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Keep Exploring