Python str.isalpha() function


In Python, the str.isalpha() method is used to check if all characters in a string are alphabetic (i.e., letters of the alphabet). This method returns True if all characters in the string are alphabetic and there is at least one character; otherwise, it returns False.

Syntax

str.isalpha()

Example Usage

  1. Basic usage with alphabetic characters:
text = "Hello" result = text.isalpha() print(result) # Output: True
  1. Including only letters:
text = "Hello123" result = text.isalpha() print(result) # Output: False
  1. Checking an empty string:
text = "" result = text.isalpha() print(result) # Output: False
  1. With spaces and punctuation:
text = "Hello World!" result = text.isalpha() print(result) # Output: False
  1. Using non-English alphabetic characters:
text = "Café" result = text.isalpha() print(result) # Output: True

Important Notes

  • The isalpha() method only considers letters defined in Unicode, so it can recognize alphabetic characters from various languages, including accented characters.
  • If the string contains any character that is not a letter (like digits, spaces, punctuation, etc.), isalpha() will return False.

Summary

  • Use str.isalpha() to check if a string contains only alphabetic characters.
  • It returns True for strings composed solely of letters and False for any string that contains numbers, spaces, or special characters.