How to check if a string starts with a number in Python
In this tutorial, we are going to learn about how to check if a string starts with a number or not in Python.
Using isdigit() method
To check if a string starts with a number, we need to access the string’s first character using slicing syntax [:1]
and call an isdigit()
method on it.
The isdigit()
method returns true if a given character in the string is a number; otherwise, it returns false.
Here is an example:
name = "1sai"
result = name[:1].isdigit()
print(result)
Output:
True