This article will discuss different ways to check if a string starts with a number in Python.
Table of Contents
- Check if a String starts with a Number using isdigit()
- Check if a String starts with a Number using Regex
Check if a String starts with a Number using isdigit()
In Python, the string class provides a member function isdigit(). Which returns True if the string contains digits only; otherwise it returns False. We can select the string’s first character as a sub-string and check if it is a digit. This way, we can confirm if a string starts with a number or not. Let’s see some examples,
Example 1:
sample_str = "7th version" # Check if String starts with a Number if sample_str[0].isdigit(): print("The String '%s' starts with a number" % (sample_str)) else: print("The String '%s' does not starts with a number" % (sample_str))
Output:
Frequently Asked:
The String '7th version' starts with a number
Here we used a string that started with a numeric character.
Example 2:
sample_str = "Sample of 11 Strings" # Check if String starts with a Number if sample_str[0].isdigit(): print("The String '%s' starts with a number" % (sample_str)) else: print("The String '%s' does not starts with a number" % (sample_str))
Output:
The String 'Sample of 11 Strings' does not starts with a number
Here we used a string with a numeric character, but the string does not start with a number.
Latest Python - Video Tutorial
Check if a String starts with a Number using Regex
In Python, the regex module provides a function search(). It accepts a regex pattern and string as arguments. It looks for given regex pattern in the given string. If a match to the pattern is found, it returns a Match object; otherwise, it returns None if no match is found. We can check if a string starts with a number using the regex pattern “^\d”.
The caret ^ has a special meaning in a regex. It is called the “anchor”. The caret “^” matches at the beginning of the text. By using “\d” after the caret “^” i.e. “^\d”, we are specifying that string must start with a digit.
Let’s see some examples,
Example 1:
import re sample_str = "7th version" # Check if String starts with a Number if re.search("^\d", sample_str) is not None: print("The String '%s' starts with a number" % (sample_str)) else: print("The String '%s' does not starts with a number" % (sample_str))
Output:
The String '7th version' starts with a number
Here we used a string that started with a numeric character.
Example 2:
import re sample_str = "The 7th version" # Check if String starts with a Number if re.search("^\d", sample_str) is not None: print("The String '%s' starts with a number" % (sample_str)) else: print("The String '%s' does not starts with a number" % (sample_str))
Output:
The String 'The 7th version' does not starts with a number
It was a negative test. Here we used a string with a numeric character, but the string does not start with a number.
Example 3:
import re sample_str = "" if re.search("^\d", sample_str) is not None: print("The String '%s' starts with a number" % (sample_str)) else: print("The String '%s' does not starts with a number" % (sample_str))
Output:
The String '' does not starts with a number
It was a negative test. Here we used an empty string.
Summary:
We learned about two different ways to check if a string starts with a number or not in Python.
Latest Video Tutorials