- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
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
Advertisements
To Continue Learning Please Login
Login with Google