Last updated: Apr 9, 2024
Reading timeยท6 min
To check if a string starts with a number:
str.isdigit()
method to check if the character is a number.str.isdigit()
method will return True
if the string starts with a
number.my_str = '248bobby' if my_str and my_str[0].isdigit(): # ๐๏ธ this runs print('The string starts with a number') else: print('The string does NOT start with a number') print('248bobby'[0].isdigit()) # ๐๏ธ True print('bobby'[0].isdigit()) # ๐๏ธ False print('_bobby'[0].isdigit()) # ๐๏ธ False
If you need to check if a string starts with a letter, click on the following subheading:
We first make sure that the string is not empty before accessing the character
at index 0
.
0
, and the last character has an index of -1
or len(my_str) - 1
.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.
print('123'.isdigit()) # ๐๏ธ True print('1.23'.isdigit()) # ๐๏ธ False (contains period) print('-123'.isdigit()) # ๐๏ธ False (contains minus)
If the character at index 0
is a digit, then the string starts with a number.
If you try to access the character at index 0
of an empty string, you'd get an
error.
and
operator as we did in the first example or use string slicing to avoid the error.my_str = '' if my_str[:1].isdigit(): print('The string starts with a number') else: # ๐๏ธ this runs print('The string does NOT start with a number')
The syntax for string slicing
is my_str[start:stop:step]
.
start
index is inclusive, whereas the stop
index is exclusive (up to, but not including).The slice selects the first character in the string if it exists, otherwise an empty string is returned.
If you need to check if each string in a list starts with a number, use a for loop.
list_of_strings = ['12ab', '34cd', '56ef'] for item in list_of_strings: if str(item)[:1].isdigit(): print(f'{item} starts with a number') else: print(f'{item} does NOT start with a number') # Output: # 12ab starts with a number # 34cd starts with a number # 56ef starts with a number
If you need to check if all strings in a list start with a number, use the
all()
function.
list_of_strings = ['12ab', '34cd', '56ef'] if all(str(item)[:1].isdigit() for item in list_of_strings): # ๐๏ธ this runs print('All strings start with a number') else: print('NOT all strings start with a number')
The all()
function will only return True
if the condition is met for all
strings in the list.
Alternatively, you can use the re.match()
method.
The re.match()
method will return a match
object if the string starts with a
number, otherwise, None
is returned.
import re def starts_with_number(string): return bool(re.match(r'\d', string)) my_str = '249bobbyhadz' if starts_with_number(my_str): # ๐๏ธ this runs print('The string starts with a number') else: print('The string does NOT start with a number') print(starts_with_number('249bobby')) # ๐๏ธ True print(starts_with_number('__bobby')) # ๐๏ธ False print(starts_with_number('')) # ๐๏ธ 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.
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.
import re def starts_with_number(string): return bool(re.match(r'\d', string))
The \d
character matches the digits [0-9]
(and many other digit characters).
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.
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.
str.startswith()
You can also use the str.startswith()
method to check if a string starts with
a specific number.
my_str = '123bobbyhadz.com' if my_str.startswith('123'): print('The string starts with the number') else: print('The string does NOT start with the number')
The
str.startswith()
method returns True
if the string starts with the provided prefix, otherwise
the method returns False
.
If the string starts with the number 123
, then True
is returned, otherwise,
False
is returned.
my_str = '123bobbyhadz.com' print(my_str.startswith('123')) # ๐๏ธ True print(my_str.startswith('456')) # ๐๏ธ False
To check if a string starts with a letter:
str.isalpha()
method to check if the character is a letter.str.isalpha()
method will return True
if the string starts with a
letter.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
.
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.
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".
This is a three-step process:
string.ascii_letters
attribute to get a string containing the ASCII
letters.in
operator to check if the string starts with an ASCII letter.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
.
re.match()
Alternatively, you can use the re.match()
method.
The re.match()
method will return a match
object if the string starts with a
letter, otherwise None
is returned.
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.
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.
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.
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.
You can learn more about the related topics by checking out the following tutorials: