isalpha() method returns true if a string only contains letters. Python
isnumeric() returns true if all characters in a string are numbers. Python
isalnum() only returns true if a string contains alphanumeric characters, without symbols.
When you’re working with strings in Python, there may be times when you want to check whether those strings contain only letters, only numbers, or only any alphanumeric characters. For instance, a program that asks a user to insert a username may want to verify that there are no special characters in the username a user chooses.
That’s where Python’s
isalnum() string methods come in. You can use these methods to check the contents of a string against certain criteria.
This tutorial will explore how to use Python’s built-in
isalnum() functions to determine whether a string contains only letters, only numbers, or only letters and numbers, respectively. We’ll also explore an example of each of these methods in Python programs.
isalpha() string method is used to check whether a string consists of only alphabetical characters. In other words,
isalpha() checks if a string contains only letters.
isalpha() method returns the Boolean value
True if every character in a string is a letter; otherwise, it returns the Boolean value
False. In Python, a space is not an alphabetical character, so if a string contains a space, the method will return
The syntax for
isalpha() is as follows:
As you can see,
isalpha() does not take in any parameters. Instead, the method is appended to the end of a string value or a variable containing a string.
Let’s walk through an example to demonstrate how this method works.
Let’s say that we are building a registration form for a scheduling app. In order to sign up, prospective users must submit their first name, surname, email address, and a password. When someone inserts a first and second name, we want to check to make sure those names only include letters so that our program can process them correctly.
We can use the
isalpha() method to verify that the name a user submits only includes letters. Here’s an example of a program that would perform this function:
first_name = input("What is your first name?") second_name = input("What is your second name?") print(first_name.isalpha()) print(second_name.isalpha())
When we run our code and insert the value
John as our first name and
8 as our second name, our program returns the following response:
What is your first name? John What is your second name? 8 True False
Let’s break down our code. On the first two lines, we use the Python input() method to collect a user’s first and second names. Then, we use the
isalpha() method to check whether these names only contain alphabetical characters. When our program evaluates
first_name.isalpha(), it returns
True because the value our program stored as
first_name contains only letters.
However, when our program evaluates the second name, it returns
False because our user inserted a number as their second name.
isnumeric() method checks whether all the characters in a string are numbers. If each character is a number,
isnumeric() returns the value
True. Otherwise, the method returns the value
The syntax for the Python
isnumeric() method is as follows:
Similar to the
isnumeric() does not accept any parameters. Instead, it is appended to the end of a string.
Let’s walk through an example to illustrate how to use
Say that we are building a multiplication game for fourth graders. Our program generates math problems for the students and asks them to type an answer into our program. However, before we can check if a user’s answer is correct, we need to check whether they inserted a number.
Here’s the code we could use to verify that a user inserted a numerical answer to the math problem they were given:
FREE Python Fundamentals Workshop
Use the calendar below to reserve your seat.
Demand for people who know Python is soaring! In this free online workshop, learn the fundamentals of Python and meet other Career Karma members who are building with Python.
student_answer = input("What is 9 x 10?") print(student_answer.isnumeric())
When we run our code and type in a number, our program returns the following response:
What is 9 x 10? 90 True
On the first line of our code, we use the
input() method to accept a student’s answer to the math problem. Note that
input() always returns a string.
On the next line of code, we use
isnumeric() to check whether the contents of the student’s answer are all numbers. In this case, the student entered
90, which is all numbers, so our program returns
Often, you’ll want to check whether strings contain only alphanumeric characters—in other words, letters and numbers. That’s where
isalnum() can be helpful.
isalnum() is a built-in Python function that checks whether all characters in a string are alphanumeric. In other words,
isalnum() checks whether a string contains only letters or numbers or both. If all characters are alphanumeric,
isalnum() returns the value
True; otherwise, the method returns the value
The syntax for the
isalnum() function is as follows:
isalnum() does not accept any parameters.
Say that we are building a registration form for a game that asks users to choose a username. We want to require that usernames contain only letters and numbers. If a user chooses a username that includes other characters, our program should present a message telling them that their username is invalid.
We could use the following code to accomplish this goal:
username = input("Choose a username:") if username.isalnum() == True: print("Your new username is ", username) else: print("This username is invalid.")
When we run our code and insert the username
user123 into our program, our program returns the following:
Choose a username: user123 Your new username is user123
If we were to insert the username
user123!, which includes a non-alphanumeric character, our program would return the following:
Choose a username: user123! This username is invalid.
When we enter the username
isalnum() method evaluates to
True, because the string only includes letters and numbers. So, the contents of our
if loop are executed, and the message
Your new username is user123 is printed to the console. But when we include a non-alphanumeric character in the username, the
isalnum() method evaluates to
False, and our program prints
This username is invalid. to the console.
When you’re working with strings, you may want to evaluate whether they contain only letters, only numbers, or only any alphanumeric characters. That’s where the
isalnum() methods come in, respectively.
Here’s a quick summary of all three:
isalpha Python is a string method that returns true or false, checking whether a string consists of only alphabetical characters.
isnumeric Python is a string method that checks whether a string consists of only numeric characters and returns true or false.
isalnum Python is a string method that checks whether a string consists of only letters and numbers, without special characters or punctuation, and returns true or false.
Now you’re ready to start using
isalnum() like a Python pro!