- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
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
Check if a given string is a valid number in Python
Suppose we have a string, that holds numeric characters and decimal points, we have to check whether that string is representing a number or not. If the input is like “2.5”, output will be true, if the input is “xyz”, output will be false.
To solve this, we will follow these steps −
- To solve this, we will use the string parsing technique of our programming language. We will try to convert string to number, if there is no exception, then that will be a number, otherwise not a number.
Example
Let us see the following implementation to get better understanding −
def isNumeric(s): s = s.strip() try: s = float(s) return True except: return False print(isNumeric("0.2")) print(isNumeric("xyz")) print(isNumeric("Hello")) print(isNumeric("-2.5")) print(isNumeric("10"))
Input
“0.2” “abc” “Hello” “-2.5” “10”
Output
True False False True True
- Related Articles
- Check if a given string is a valid number in C++
- Java Program to check if a string is a valid number
- Check whether the given string is a valid identifier in Python
- How to check if a string is a valid keyword in Python?
- Python program to check if a given string is number Palindrome
- How to check if a string is a valid keyword in C#?
- How to check if a string is a valid keyword in Java?
- How to check if a string is a valid URL in Golang?
- Python - Check if a given string is binary string or not
- Check whether a string is valid JSON or not in Python
- Check whether triangle is valid or not if sides are given in Python
- How to check if a given number is a Fibonacci number in Python Program ?
- Python Program to check if a substring is present in a given string.
- Check if a given string is sum-string in C++
- Python program to check if the given number is a Disarium Number

Advertisements