Check if a string starts with a Letter in Python

avatar

Borislav Hadzhiev

Last updated: Sep 20, 2022

banner

Photo from Unsplash

Check if a string starts with a Letter in Python #

To check if a string starts with a letter:

  1. Access the first character in the string.
  2. Use the str.isalpha() method to check if the character is a letter.
  3. The str.isalpha() method will return True if the string starts with a letter.
main.py
my_str = 'bobby hadz' if my_str and my_str[0].isalpha(): # ๐Ÿ‘‡๏ธ this runs print('The string starts with a letter') else: print('The string does NOT start with a letter') print('bobby'[0].isalpha()) # ๐Ÿ‘‰๏ธ True print('_bobby'[0].isalpha()) # ๐Ÿ‘‰๏ธ False

We first make sure that the string is not empty before accessing the character at index 0.

Python indexes are zero-based, so the first character in a string has an index of 0, and the last character has an index of -1 or len(my_str) - 1.

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('BOBBY'.isalpha()) # ๐Ÿ‘‰๏ธ True print('_BOBBY'.isalpha()) # ๐Ÿ‘‰๏ธ False

The str.isalpha method considers alphabetic characters the ones that are defined in the Unicode character database as "Letter".

To check if a string starts with an ASCII letter:

  1. Use the string.ascii_letters attribute to get a string containing the ASCII letters.
  2. Access the first character in the original string.
  3. Use the in operator to check if the string starts with an ASCII letter.
main.py
import string my_str = 'bobby hadz' ascii_letters = string.ascii_letters # ๐Ÿ‘‡๏ธ abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ print(ascii_letters) if my_str and my_str[0] in ascii_letters: # ๐Ÿ‘‡๏ธ this runs print('The string starts with an ASCII letter') else: print('The string does NOT start with an ASCII letter') print('bobby'[0] in ascii_letters) # ๐Ÿ‘‰๏ธ True print('ั„ั„ั„ bobby'[0] in ascii_letters) # ๐Ÿ‘‰๏ธ False

The string.ascii_letters attribute returns a string containing the lowercase and uppercase ASCII letters.

The in operator tests for membership. For example, x in s evaluates to True if x is a member of s, otherwise it evaluates to False.

Alternatively, you can use the re.match() method.

Check if a string starts with a Letter in Python #

Use the re.match() method to check if a string starts with a letter, e.g. re.match(r'[a-zA-Z]', string). The re.match() method will return a match object if the string starts with a letter, otherwise None is returned.

main.py
import re def starts_with_letter(string): return bool(re.match(r'[a-zA-Z]', string)) if starts_with_letter('bhadz'): print('The string starts with a letter') else: print('The string does NOT start with a letter') print(starts_with_letter('bhadz')) # ๐Ÿ‘‰๏ธ True print(starts_with_letter('__bhadz')) # ๐Ÿ‘‰๏ธ False print(starts_with_letter('444bhadz')) # ๐Ÿ‘‰๏ธ False print(starts_with_letter('ะคะค bhadz')) # ๐Ÿ‘‰๏ธ False

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 doesn't match the regex pattern.

We used the bool() class to convert the match object or None value to a boolean.

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

main.py
import re def starts_with_letter(string): return bool(re.match(r'[a-zA-Z]', string))

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 re.match() method only checks if the characters at the beginning of the string match the regular expression, so it does exactly what we need.

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.

Copyright ยฉ 2022 Borislav Hadzhiev