In this tutorial, we will look at how to convert a string in Python to a float value, that is, `float`

datatype with the help of some examples.

## How to convert string to float in Python?

You can use the Python built-in `float()`

function to convert a string to a float value in Python. The following is the syntax –

# convert string s to float float(s)

It returns the passed string as a floating-point value. Note that if the passed string does not represent a numeric value, it will raise an error.

## Examples

Let’s look at some examples of using the above syntax to convert a string to a floating-point value.

First, let’s apply the `float()`

function on a string containing a real number with a decimal part, for example, “150.75”.

# string storing a float number s = "150.75" # convert string to float num = float(s) # display the number and its type print(num) print(type(num))

Output:

150.75 <class 'float'>

You can see that we get `150.75`

as a float value.

What happens if you pass an integer number as a string to the `float()`

function? Let’s find out.

**Data Science Programs By Skill Level**

**Introductory** ⭐

- Harvard University Data Science: Learn R Basics for Data Science
- Standford University Data Science: Introduction to Machine Learning
- UC Davis Data Science: Learn SQL Basics for Data Science
- IBM Data Science: Professional Certificate in Data Science
- IBM Data Analysis: Professional Certificate in Data Analytics
- Google Data Analysis: Professional Certificate in Data Analytics
- IBM Data Science: Professional Certificate in Python Data Science
- IBM Data Engineering Fundamentals: Python Basics for Data Science

**Intermediate ⭐⭐⭐**

- Harvard University Learning Python for Data Science: Introduction to Data Science with Python
- Harvard University Computer Science Courses: Using Python for Research
- IBM Python Data Science: Visualizing Data with Python
- DeepLearning.AI Data Science and Machine Learning: Deep Learning Specialization

**Advanced ⭐⭐⭐⭐⭐**

- UC San Diego Data Science: Python for Data Science
- UC San Diego Data Science: Probability and Statistics in Data Science using Python
- Google Data Analysis: Professional Certificate in Advanced Data Analytics
- MIT Statistics and Data Science: Machine Learning with Python - from Linear Models to Deep Learning
- MIT Statistics and Data Science: MicroMasters® Program in Statistics and Data Science

**🔎 Find Data Science Programs 👨💻 111,889 already enrolled**

Disclaimer: Data Science Parichay is reader supported. When you purchase a course through a link on this site, we may earn a small commission at no additional cost to you. Earned commissions help support this website and its team of writers.

# string storing an integer s = "150" # convert string to float num = float(s) # display integer and its type print(num) print(type(num))

Output:

150.0 <class 'float'>

We get `150.0`

as a float point number after conversion.

If you want to convert the above string to an integer in Python, use the `int()`

function instead.

# string storing an integer s = "150" # convert string to int num = int(s) # display number and its type print(num) print(type(num))

Output:

150 <class 'int'>

We get the string as an integer.

What happens if you pass a non-numeric string to the `float()`

function? Let’s find out.

# string with text s = "cat" # convert string to float num = float(s) # display number and its type print(num) print(type(num))

Output:

--------------------------------------------------------------------------- ValueError Traceback (most recent call last) Input In [4], in <module> 2 s = "cat" 3 # convert string to float ----> 4 num = float(s) 5 # display number and its type 6 print(num) ValueError: could not convert string to float: 'cat'

As expected, we get an error because the passed string cannot be represented as a floating-point number.

You might also be interested in –

- Extract Numbers From String in Python
- Python – Check if String Contains Only Numbers
- Python Tuple to Integer

**Subscribe to our newsletter for more informative guides and tutorials. ****We do not spam and you can opt out any time.**