
- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions
How do we check in Python whether a string contains only numbers?
Python has an in-built function isdigit() which returns true if all characters in a string are digit (between 0-9)
>>> string='9764135408' >>> string.isdigit() True >>> string='091-9764135408' >>> string.isdigit() False
You can also use regex expression to check if string contains digits only.
>>> import re >>> bool(re.match('^[0-9]+$','9764135408')) True >>> bool(re.match('^[0-9]+$','091-9764135408')) False
- Related Articles
- Check whether the String contains only digit characters in Java
- How to check if a Python string contains only digits?
- How do I verify that a string only contains letters, numbers, underscores and dashes in Python?
- How to check if a string only contains certain characters in Python?
- How to check if a string contains only whitespace letters in Python?
- How do we check if a String contains a substring (ignoring case) in Java?
- How to check if a unicode string contains only numeric characters in Python?
- How to check if a string contains only lower case letters in Python?
- How to check if a string contains only upper case letters in Python?
- How to check whether a string contains a substring in JavaScript?
- How to check whether a string contains a substring in jQuery?
- How to check if a string contains only decimal characters?
- How to check whether a String contains a substring or not?
- Check if a string contains numbers in MySQL?
- How do I check whether a field contains null value in MongoDB?

Advertisements