Only allow letters when taking user Input in Python

avatar

Borislav Hadzhiev

Last updated: Aug 23, 2022

banner

Photo from Unsplash

Only allow letters when taking user Input in Python #

To only allow letters when taking user input:

  1. Use a while loop to iterate until the user enters only letters.
  2. Use the str.isalpha() method to check if the user entered only letters.
  3. If the condition is met, break out of the loop.
main.py
user_input = '' while True: user_input = input('Enter letters only: ') if not user_input.isalpha(): print('Enter only letters') continue else: print(user_input) break

only allow letters when taking user input

We used a while loop to iterate until the user enters only letters.

On each iteration, we check if the user didn't enter only letters and use a continue statement if the condition is met.

The continue statement continues with the next iteration of the loop.

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 # 👇️ contains space print('one two'.isalpha()) # 👉️ False
If you also want to allow spaces, scroll down to the last code snippet.

If the user entered only letters, we print the input value and break out of the loop.

The break statement breaks out of the innermost enclosing for or while loop.

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

Alternatively, you can use a regular expression.

main.py
import re user_input = '' while True: user_input = input('Enter letters only: ') if not re.match(r'^[a-zA-Z]+$', user_input): print('Enter only letters') continue else: print(user_input) break

The code snippet achieves the same result but uses a regular expression to validate the user input.

If you also want to allow spaces, use the following regular expression instead.

main.py
import re user_input = '' while True: user_input = input('Enter letters only: ') # 👇️ also allows spaces if not re.match(r'^[a-zA-Z\s]+$', user_input): print('Enter only letters') continue else: print(user_input) break

The re.match method returns a match object if the provided regular expression is matched in the string.

The match method returns None if the string does not match the regex pattern.

The first argument we passed to the re.match method is a regular expression.

The square brackets [] are used to indicate a set of characters.

The a-z and A-Z characters represent lowercase and uppercase ranges of letters.

The caret ^ matches the start of the string and the dollar sign $ matches the end of the string.

The plus + causes the regular expression to match 1 or more repetitions of the preceding character (the letter ranges).

main.py
import re user_input = '' while True: user_input = input('Enter letters only: ') # 👇️ also allows spaces if not re.match(r'^[a-zA-Z\s]+$', user_input): print('Enter only letters') continue else: print(user_input) break

The \s character matches unicode whitespace characters like [ \t\n\r\f\v].

If the user didn't enter only letters, we use the continue statement to prompt them for input again.

Otherwise, we use the break statement to break out of the while loop.

If you ever need help reading or writing a regular expression, consult the regular expression syntax subheading in the official docs.

The page contains a list of all of the special characters with many useful examples.

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.