Check if input contains only letters in Python

avatar

Borislav Hadzhiev

Last updated: Aug 22, 2022

banner

Photo from Unsplash

Check if input contains only letters in Python #

Use the str.isalpha() method to check if a user input contains only letters, e.g. if user_input.isalpha():. The isalpha method will return True if all characters in the input are alphabetic and it contains at least one character.

main.py
# ✅ Check if input contains only letters user_input = input('Enter a character: ') if user_input.isalpha(): print('Input contains only letters') # ----------------------------------------- # ✅ Check if input contains a single letter user_input = input('Enter a character: ') if len(user_input) == 1 and user_input.isalpha(): print('Input contains only letters') # ------------------------------------------ # ✅ Check if input contains only digits user_input = input('Enter a digit: ') if user_input.isdigit(): print('Input contains only digits') num = int(user_input) print(num)

The first example uses the str.isalpha() method to check if an input stores only letters.

main.py
user_input = input('Enter a character: ') if user_input.isalpha(): print('Input contains only letters')

The str.isalpha() method returns True if all characters in the string are alphabetic and there is at least one character, otherwise False is returned.

main.py
print('avocado'.isalpha()) # 👉️ True print('one two'.isalpha()) # 👉️ False

If you need to check if an input value stores exactly 1 letter, use the len() function.

main.py
user_input = input('Enter a character: ') if len(user_input) == 1 and user_input.isalpha(): print('Input contains only letters')

The len() function returns the length (the number of items) of an object.

The argument the function takes may be a sequence (a string, tuple, list, range or bytes) or a collection (a dictionary, set, or frozen set).

If you need to check if the input value is an integer, use the str.isdigit() method.

main.py
user_input = input('Enter a digit: ') if user_input.isdigit(): print('Input contains only digits') num = int(user_input) print(num)

The str.isdigit method returns True if all characters in the string are digits and there is at least 1 character, otherwise False is returned.

main.py
print('123'.isdigit()) # 👉️ True print('2.5'.isdigit()) # 👉️ False print('-123'.isdigit()) # 👉️ False

Note that the str.isdigit() method returns False for negative integers and floating-point numbers.

This is because negative integers contain a minus sign and floating-point numbers contain a period.

The input function takes an optional prompt argument and writes it to standard output without a trailing newline.

The function then reads the line from input, converts it to a string and returns the result.

The return value of the input() function is guaranteed to be a string even if the user enters a number.

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.