
- 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 to check if a string can be converted to float in Python?
In this article, we are going to focus on checking if a string can be converted to a float in Python.
The first approach is by using the float() type cast. We will write a function using exception handling and check if the given string can be converted into string, if it can be converted then True is returned, else False is returned.
A floating−point number is returned by the method float() for a given number or text. It will return 0.0 as the floating−point value if no value or a blank parameter is given.
Example 1
In this example, we are taking a string as input and we are checking if it can be converted into float using the float() type casting −
def isFloat(s): try: float(s) return True except: return False str1 = "36.9" print("The given string is") print(str1) print("Checking if the given string can be converted into float") res = isFloat(str1) print(res)
Output
The output of the above example is as shown below −
The given string is 36.9 Checking if the given string can be converted into float True
Example 2
In the example given below, we are taking the same program as above and we are taking different input and checking if it can be converted to float −
def isFloat(s): try: float(s) return True except: return False str1 = "Welcome" print("The given string is") print(str1) print("Checking if the given string can be converted into float") res = isFloat(str1) print(res)
Output
The output of the above example is given below −
The given string is Welcome Checking if the given string can be converted into float False
Using isdigit() and replace() method
The second approach is by using isdigit() and replace(). We will replace the ‘.’ present in the float with blank space and checks if the resultant string with isdigit(). If all the characters are digit then True is returned, else False is returned.
Example 1
In this example, we are taking a string as input and checking if it can be converted to float using isdigit() and replace() methods −
str1 = "69.3" print("The give string is") print(str1) print("Checking if the given string can be converted into float") res = str1.replace('.', '', 1).isdigit() print(res)
Output
The output of the above example is as shown below −
The give string is 69.3 Checking if the given string can be converted into float True
Example 2
In the example given below, we are taking the same program as above but we are taking a different input and checking if it can be converted into a float −
str1 = "Welcome" print("The give string is") print(str1) print("Checking if the given string can be converted into float") res = str1.replace('.', '', 1).isdigit() print(res)
Output
The output of the above example is as follows −
The give string is Welcome Checking if the given string can be converted into float False
- Related Articles
- Check if a string can be converted to another string by replacing vowels and consonants in Python
- Program to check one string can be converted to another by removing one element in Python
- Program to check one string can be converted to other by shifting characters clockwise in Python
- Check if a string can be repeated to make another string in Python
- Check if matrix can be converted to another matrix by transposing square sub-matrices in Python
- Check if a string can be rearranged to form special palindrome in Python
- Program to check whether one point can be converted to another or not in Python
- Check if matrix A can be converted to B by changing parity of corner elements of any submatrix in Python
- How to check if a float value is a whole number in Python?
- Check if characters of a given string can be rearranged to form a palindrome in Python
- Check if characters of one string can be swapped to form other in Python
- Check if a string can be formed from another string using given constraints in Python
- Check if a string can be obtained by rotating another string 2 places in Python
- How can RGB color space be converted to a different color space in Python?
- Check if a two-character string can be made using given words in Python
