How do i convert a float to a string in python?

Shahpar Khan

Datatypes

In some languages, the developer needs to explicitly specify the datatype. For example, in C++, variables are declared by first specifying the datatype and then following it with the variable name:

int my_int;
float my_float;
string my_string;

On the other hand, in languages like JavaScript or Python, there is no need to explicitly mention a variable’s datatype at the time of declaration as the datatype is set according to the value assigned to it.

#JavaScript
var my_int = 123
var my_float = 123.123
var my_string = "123"

#Python
my_int = 123
my_float = 123.123
my_string = "123"

Later, after the variable’s initial declaration, it can be type-casted in a different datatype. Type-casting comes in handy when avoiding ambiguous comparisons between different data types and operators/functions performing unexpectedly.

######## FAULTY COMPARISON ########

my_float = 123.123
my_string = "123.123"

equal = (my_float == my_string)

if equal:
  print("Variables are equal")

else:
  print("Variables are not equal")

######## UNEXPECTED FUNCTIONALITY ########

#Want to concatenate 123.0 and string "my float is: "

#Throws an error 

my_float = 123.0
my_string = "my float is: " 

result = my_string + my_float

print(result)

Possible errors

To deal with such scenarios, we can easily convert datatypes in Python using type-casting:

SyntaxDescription
str() Cast to string
float() Cast to float
int() Cast to int

To check the type of a variable, use the type() method.

In the code snippet below, observe how we can avoid the error in the previous example by type-casting float to string in Python:

#Want to concatenate 123.0 and string "my float is: "

my_float = 123.0
my_string = "my float is: " 

result = my_string + str(my_float)

print(result)

Casting float to string

CONTRIBUTOR

Shahpar Khan

Copyright ©2022 Educative, Inc. All rights reserved

We can convert a string to float in Python using the float() function. This is a built-in function used to convert an object to a floating point number. Internally, the float() function calls specified object __float__() function.

Python Convert String to float

Let’s look at a simple example to convert a string to float in Python.

s = '10.5674'

f = float(s)

print(type(f))
print('Float Value =', f)

Output:


Float Value = 10.5674

Why do we need to convert a string to float?

If we are getting float value from user input through the terminal or reading it from a file, then they are string objects. We have to explicitly convert them to float so that we can perform necessary operations on it, such as addition, multiplication, etc.

input_1 = input('Please enter first floating point value:\n')
input_1 = float(input_1)

input_2 = input('Please enter second floating point value:\n')
input_2 = float(input_2)

print(f'Sum of {input_1} and {input_2} is {input_1+input_2}')

How do i convert a float to a string in python?

Ideally, we should use try-except block to catch exceptions in case of invalid input from user. If you are not familiar with string formatting using f prefix, please read f-strings in Python.

Python Convert float to String

We can convert float to a string easily using str() function. This might be required sometimes where we want to concatenate float values. Let’s look at a simple example.

f1 = 10.23
f2 = 20.34
f3 = 30.45

# using f-string from Python 3.6+, change to format() for older versions
print(f'Concatenation of {f1} and {f2} is {str(f1) + str(f2)}')
print(f'CSV from {f1}, {f2} and {f3}:\n{str(f1)},{str(f2)},{str(f3)}')
print(f'CSV from {f1}, {f2} and {f3}:\n{", ".join([str(f1),str(f2),str(f3)])}')

Output:

Concatenation of 10.23 and 20.34 is 10.2320.34
CSV from 10.23, 20.34 and 30.45:
10.23,20.34,30.45
CSV from 10.23, 20.34 and 30.45:
10.23, 20.34, 30.45

If we don’t convert float to string in the above program, the join() function will throw an exception. Also, we won’t be able to use + operator to concatenate as it will add the floating point numbers.

You can checkout complete python script and more Python examples from our GitHub Repository.

Reference: float() official documentation

How do you convert a float to a string?

We can convert float to String in java using String. valueOf() and Float..
public class FloatToStringExample2{.
public static void main(String args[]){.
float f=89. 7F;.
String s=Float. toString(f);.
System. out. println(s);.

How do I change the float format in Python?

Format float value using the round() Method in Python The round() is a built-in Python method that returns the floating-point number rounded off to the given digits after the decimal point. You can use the round() method to format the float value.

How do I convert to string in Python?

In Python an integer can be converted into a string using the built-in str() function. The str() function takes in any python data type and converts it into a string.

How do you turn a float into a value?

Integer and Float Conversions To convert the integer to float, use the float() function in Python. Similarly, if you want to convert a float to an integer, you can use the int() function.