• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

The Programming Expert

Solving All of Your Programming Headaches

  • HTML
  • JavaScript
  • jQuery
  • PHP
  • Python
  • SAS
  • Ruby
  • About
You are here: Home / Python / Convert String to Float with float() in Python

Convert String to Float with float() in Python

August 13, 2022 Leave a Comment

To convert a string variable to a float in Python, use the float() function.

x = "1"
y = float(x)

print(y)

#Output:
1.0

When working with string variables in Python, the ability to easily be able to use and change these variables is valuable.

One such situation is if you want to create a float from a string variable.

To convert a string variable to a float in Python, use the float() function.

float() takes a string variable which represents a floating-point number, such as “12.2” or “4.9”, and returns a float variable.

Below shows a few simple examples of how you can use float() to convert a string to a float in Python.

a = "1"
b = "12.2"
c = "43.001"

print(float(a))
print(float(b))
print(float(c))

#Output:
1.0
12.2
43.001

Using float() to Convert String to Float in Python

To use float(), there are a few things you should know. First, you need to pass a string literal representing a floating-point number.

For example, if you try to pass a string which has letters, then you will get a ValueError.

If you pass a string representing an integer to float, you will get the floating-point representation of that integer. If you instead what to convert a string to an integer in Python, use int().

Below shows a few more examples of how to use float() and shows some common errors which might come from the use of float().

a = "1"
b = "1.23"
c = "abc"

print(float(a))
print(float(b))
print(float(c))

#Output:
1.0
1.23
ValueError: could not convert string to float: 'abc'

Hopefully this article has been useful for you to learn how to convert a string to float with Python.

Other Articles You'll Also Like:

  • 1.  Draw Star in Python Using turtle Module
  • 2.  Python Destroy Object – How to Delete Objects with del Keyword
  • 3.  Drop Last Row of pandas DataFrame
  • 4.  Reverse a List in Python Without Reverse Function
  • 5.  Python Replace Space with Underscore Using String replace() Function
  • 6.  Using Python to Print Environment Variables
  • 7.  Truncate String in Python with String Slicing
  • 8.  pandas unique – Get Unique Values in Column of DataFrame
  • 9.  Count Number of Keys in Dictionary in Python
  • 10.  Get pandas Column Names as List in Python

About The Programming Expert

The Programming Expert is a compilation of a programmer’s findings in the world of software development, website creation, and automation of processes.

Programming allows us to create amazing applications which make our work more efficient, repeatable and accurate.

At the end of the day, we want to be able to just push a button and let the code do it’s magic.

You can read more about us on our about page.

Reader Interactions

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Primary Sidebar

About The Programming Expert

the programming expert main image

Welcome to The Programming Expert. We are a group of US-based programming professionals who have helped companies build, maintain, and improve everything from simple websites to large-scale projects.

We built The Programming Expert to help you solve your programming problems with useful coding methods and functions in various programming languages.

Search

Learn Coding from Experts on Udemy

Looking to boost your skills and learn how to become a programming expert?

Check out the links below to view Udemy courses for learning to program in the following languages:

Copyright © 2023 · The Programming Expert · About · Privacy Policy