Skip to content
  • (+91) 9409548155
  • support@appdividend.com
  • Home
  • Pricing
  • Instructor
  • Tutorials
    • Laravel
    • Python
    • React
    • Javascript
    • Angular
  • Become A Tutor
  • About Us
  • Contact Us
Menu
  • Home
  • Pricing
  • Instructor
  • Tutorials
    • Laravel
    • Python
    • React
    • Javascript
    • Angular
  • Become A Tutor
  • About Us
  • Contact Us
  • Home
  • Pricing
  • Instructor
  • Tutorials
    • Laravel
    • Python
    • React
    • Javascript
    • Angular
  • Become A Tutor
  • About Us
  • Contact Us
Python

How to Convert Float (Double) to String in Python

  • 10 Oct, 2025
  • Com 0
Python Float to String

The simplest and most efficient way to convert a float (double) value to a string in Python is to use the built-in str() function or f-string (Python 3.6+).

Method 1: Using str()

It converts an input to its string representation using a default precision (approximately up to 15 decimal places, trimming trailing zeros).

Using str() Method to convert a float to string in Python

float_value = 3.14159

print(float_values)
# Output: 3.14159

print(type(float_value))
# Output: <class 'float'>

string_value = str(float_value)

print(string_value)
# Output: "3.14159"

print(type(string_value))
# Output: <class 'str'>

# Negative number
negative_float = -21.19

print(negative_float)
# Output: -21.19

print(type(negative_float))
# Output: <class 'float'>

string_float = str(negative_float)

print(string_float)
# Output: "-21.19"

print(type(string_float))
# Output: <class 'str'>

 

In this code, we passed a positive float value and a negative float value, and in both cases, it returns the respective string.

We can verify the data type using the type() method. Before the conversion, the type of a variable was <class ‘float’>, and after the conversion, the type is <class ‘str’>.

Extra large/small numbers

If the input is a very large or small floating value, it returns the shortest string (16 digits) that, when converted back to a float, yields the exact binary representation.

big_float_value = 3.1415926535897932384626433832795

print(type(big_float_value))
# Output: <class 'float'>

max_string = str(big_float_value)

print(max_string)
# Output: '3.141592653589793'

print(type(max_string))
# Output: <class 'str'>

small_float_value = 1.23e-10

print(type(small_float_value))
# Output: <class 'float'>

min_string = str(small_float_value)

print(min_string)
# Output: '1.23e-10'

print(type(min_string))
# Output: <class 'str'>

Here, you can see that when we pass a value with scientific notation, such as “e”, it returns the output in that exact format.

Special values (NaN, Infinity)

What if the input values are NaN or infinity? In this case, it returns the string representation of nan and inf values, which are case sensitive.

import math

nan_val = math.nan
print(type(nan_val))
# Output: <class 'float'>

inf_val = float('inf')
print(type(inf_val))
# Output: <class 'float'>

print(str(nan_val))
# Output: 'nan'
print(type(str(nan_val)))
# Output: <class 'str'>

print(str(inf_val))
# Output: 'inf'
print(type(str(inf_val)))
# Output: <class 'str'>

neg_inf = float('-inf')
print(type(neg_inf))
# Output: <class 'float'>

print(str(neg_inf))
# Output: '-inf'
print(type(str(neg_inf)))
# Output: <class 'str'>

Method 2: Using f-string

The f-string was introduced in Python 3.6 as a straightforward way to format a string. With its help, we can easily control the formatting, such as specifying the number of decimal places, width, or notation, when converting a double to a string.

Using f-string to format a float value to string

f_value = 3.1415926535

print(f_value)
# Output: 3.1415926535

print(type(f_value))
# Output: <class 'float'>

string_frmt = "{:.2f}".format(f_value)

print(string_frmt)
# Output: '3.14'

print(type(string_frmt))
# Output: <class 'str'>

Padding

We can add spaces to the input value while converting to a string and format it using an f-string.

Padded string in Python

num = 42.38

print(num)
# Output: 42.38

print(type(num))
# Output: <class 'float'>

padded_string = "{:10.2f}".format(num)

# Right-aligned, width 10
print(padded_string)
# Output: '     42.38' (added spaces)

print(type(padded_string))
# Output: <class 'str'>

That’s all!

Post Views: 17
Share on:
Krunal Lathiya

With a career spanning over eight years in the field of Computer Science, Krunal’s expertise is rooted in a solid foundation of hands-on experience, complemented by a continuous pursuit of knowledge.

How to Close a File in Python
How to Convert Set to String in Python

Leave a Reply Cancel reply

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

Address: TwinStar, South Block – 1202, 150 Ft Ring Road, Nr. Nana Mauva Circle, Rajkot(360005), Gujarat, India

Call: (+91) 9409548155

Email: support@appdividend.com

Online Platform

  • Pricing
  • Instructors
  • FAQ
  • Refund Policy
  • Support

Links

  • About Us
  • Contact Us
  • Privacy Policy
  • Terms of services

Tutorials

  • Angular
  • React
  • Python
  • Laravel
  • Javascript
Copyright @2024 AppDividend. All Rights Reserved
Appdividend