Change language

Python Regular Expression | Check if input is floating point or not

| |

Given the input, write a Python program to check if the given input is a floating point number or not.

Examples :

  Input:  1.20  Output:  Floating point number  Input:  -2.356  Output:  Floating point number  Input:  0.2  Output:  Floating point number  Input:  -3  Output:  Not a Floating point number 

In this program we are using the search() module re method.

re.search (): this method either returns None (if the pattern does not match) or re.MatchObject which contains information about the matching part of the string. This method stops after the first match, so it is better suited for validating regular expressions than retrieving data.

Let’s take a look at a Python program to do this:

# Python program to validate input
# Floating point number or not

 
# import re module

  
# re module provides support
# for regular expressions

import re

  
# Make regular expression for
# float definition

< code class = "plain"> regex = ’[+ -]? [0-9] +. [0-9 ] + ’

  
# Define a function for
# check floating point number

def check (floatnum ): 

 

# pass a regular expression

# and a string in the search () method

if (re.search (regex, floatnum)): 

print ( "Floating point number"

  

else

print ( " Not a Floating point number "

  

 
Driver code

if __ name__ = = ’__main__’

  

# Enter a floating point number

floatnum = "1.20"

  

# call the run function

  check (floatnum)

  

floatnum = "- 2.356 "

  check (floatnum)

 

floatnum = " 0.2 "

  check (floatnum)

 

floatnum = "- 3"

check (floatnum)

Exit:

 Floating point number Floating point number Floating point number Not a Floating point number 

Shop

Learn programming in R: courses

$

Best Python online courses for 2022

$

Best laptop for Fortnite

$

Best laptop for Excel

$

Best laptop for Solidworks

$

Best laptop for Roblox

$

Best computer for crypto mining

$

Best laptop for Sims 4

$

Latest questions

NUMPYNUMPY

psycopg2: insert multiple rows with one query

12 answers

NUMPYNUMPY

How to convert Nonetype to int or string?

12 answers

NUMPYNUMPY

How to specify multiple return types using type-hints

12 answers

NUMPYNUMPY

Javascript Error: IPython is not defined in JupyterLab

12 answers

News


Wiki

Python OpenCV | cv2.putText () method

numpy.arctan2 () in Python

Python | os.path.realpath () method

Python OpenCV | cv2.circle () method

Python OpenCV cv2.cvtColor () method

Python - Move item to the end of the list

time.perf_counter () function in Python

Check if one list is a subset of another in Python

Python os.path.join () method