Python String isalpha() Method – Check for Alphabetic Characters

Python String isalpha() Method

The isalpha() method in Python checks if all characters in a string are alphabetic (letters A-Z and a-z). It's useful for input validation, data filtering, and more.

Syntax

string.isalpha()

Parameters:

  • No parameters required.

Returns:

  • True – If all characters are alphabetic and the string is not empty.
  • False – If there are any non-alphabetic characters (numbers, spaces, symbols, etc.) or if the string is empty.

Example 1: All Letters

text = "Python"
print(text.isalpha())
True

Example 2: Contains Numbers

text = "Python3"
print(text.isalpha())
False

Example 3: Contains Spaces

text = "Hello World"
print(text.isalpha())
False

Example 4: Empty String

text = ""
print(text.isalpha())
False

Use Cases

  • Validating if a name contains only letters
  • Pre-checking strings before using them in word-based logic
  • Filtering out invalid words from a dataset

Note on Unicode Characters

isalpha() returns True for letters in other languages too. For example:

print("नमस्ते".isalpha())
True

Common Mistakes

  • Using it to check for alphanumeric strings (use isalnum() instead).
  • Expecting it to ignore spaces – it doesn't.

Interview Tip

isalpha() is commonly used in input sanitization and parsing problems in coding interviews and online judges.

Summary

  • isalpha() checks if all characters in the string are letters.
  • Returns False if there are spaces, digits, or symbols.
  • Returns False on an empty string.

Practice Problem

Ask the user to enter a name and print whether it is valid (contains only letters):

name = input("Enter your name: ")
if name.isalpha():
    print("Valid name!")
else:
    print("Invalid name. Please use only letters.")