Open In App

Python regex | Check whether the input is Floating point number or not

Last Updated : 25 Jan, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

Prerequisite: Regular expression in Python Given an input, write a Python program to check whether the given Input is 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 search() method of re module. re.search() : This method either returns None (if the pattern doesn’t match), or re.MatchObject that contains information about the matching part of the string. This method stops after the first match, so this is best suited for testing a regular expression more than extracting data. Let’s see the Python program for this : 

Python3




# Python program to check input is
# Floating point number or not
 
# import re module
 
# re module provides support
# for regular expressions
import re
 
# Make a regular expression for
# identifying Floating point number
regex = '[+-]?[0-9]+\.[0-9]+'
 
# Define a function to
# check Floating point number
def check(floatnum):
 
     # pass the regular expression
     # and the string in search() method
    if(re.search(regex, floatnum)):
        print("Floating point number")
         
    else:
        print("Not a Floating point number")
     
 
# Driver Code
if __name__ == '__main__' :
     
    # Enter the floating point number
    floatnum = "1.20"
     
    # calling run function
    check(floatnum)
 
    floatnum = "-2.356"
    check(floatnum)
 
    floatnum = "0.2"
    check(floatnum)
 
    floatnum = "-3"
    check(floatnum)


Output:

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

Time complexity: O(n), where n is the length of the input string (floatnum). 

  • The re.search() function is used to match the regular expression pattern with the input string. The time complexity of this function is O(n), where n is the length of the input string. 
  • As we are calling this function once for each input string, the overall time complexity of the code is O(n).

Auxiliary Space:  O(1) as we are not using any data structures to store any values.



Similar Reads

Floating point error in Python
Python, a widely used programming language, excels in numerical computing tasks, yet it is not immune to the challenges posed by floating-point arithmetic. Floating-point numbers in Python are approximations of real numbers, leading to rounding errors, loss of precision, and cancellations that can throw off calculations. We can spot these errors by
8 min read
Compute the natural logarithm of one plus each element in floating-point accuracy Using NumPy
Let's see the program for computing the natural logarithm of one plus each element of a given array in floating-point accuracy using NumPy library. For doing this task we are using numpy.log1p() function of NumPy. This function returns the array of natural logarithm of one plus each element of the input array. Syntax: numpy.log1p(arr, out = None, *
1 min read
Python3 Program to Check whether all the rotations of a given number is greater than or equal to the given number or not
Given an integer x, the task is to find if every k-cycle shift on the element produces a number greater than or equal to the same element. A k-cyclic shift of an integer x is a function that removes the last k digits of x and inserts them in its beginning. For example, the k-cyclic shifts of 123 are 312 for k=1 and 231 for k=2. Print Yes if the giv
3 min read
Connect new point to the previous point on a image with a straight line in Opencv-Python
OpenCV-Python is a library of Python bindings designed to solve computer vision problems. Let's see how to Connect a new point to the previous point on an image with a straight line. Step 1: Draw points on image: On a image we can mark points using cv2.circle( ) method. This method is used to draw a circle on any image. Syntax: cv2.circle(image, ce
3 min read
Python | Check if string matches regex list
Sometimes, while working with Python, we can have a problem we have list of regex and we need to check a particular string matches any of the available regex in list. Let's discuss a way in which this task can be performed. Method : Using join regex + loop + re.match() This task can be performed using combination of above functions. In this, we cre
4 min read
How to check if a string starts with a substring using regex in Python?
Prerequisite: Regular Expression in Python Given a string str, the task is to check if a string starts with a given substring or not using regular expression in Python. Examples: Input: String: "geeks for geeks makes learning fun" Substring: "geeks" Output: True Input: String: "geeks for geeks makes learning fun" Substring: "makes" Output: FalseChe
3 min read
How to check a valid regex string using Python?
A Regex (Regular Expression) is a sequence of characters used for defining a pattern. This pattern could be used for searching, replacing and other operations. Regex is extensively utilized in applications that require input validation, Password validation, Pattern Recognition, search and replace utilities (found in word processors) etc. This is du
6 min read
Python - Check if String Contain Only Defined Characters using Regex
In this article, we are going to see how to check whether the given string contains only a certain set of characters in Python. These defined characters will be represented using sets. Examples: Input: ‘657’ let us say regular expression contains the following characters- (‘78653’) Output: Valid Explanation: The Input string only consists of charac
2 min read
Check whether a number has consecutive 0's in the given base or not
Given a decimal number N, the task is to check if a number has consecutive zeroes or not after converting the number to its K-based notation. Examples: Input: N = 4, K = 2 Output: No 4 in base 2 is 100, As there are consecutive 2 thus the answer is No. Input: N = 15, K = 8 Output: Yes 15 in base 8 is 17, As there are no consecutive 0 so the answer
11 min read
Python program to represent floating number as hexadecimal by IEEE 754 standard
Prerequisite : IEEE Standard 754 Floating Point NumbersGiven a floating point number, the task is to find the hexadecimal representation for the number by IEEE 754 standard.The IEEE Standard for Floating-Point Arithmetic (IEEE 754) is a technical standard for floating-point computation which was established in 1985 by the Institute of Electrical an
3 min read
three90RightbarBannerImg