Python - How To Python Convert Float to String

ID : 110

viewed : 72

Tags : PythonPython FloatPython String

vote vote

92

This tutorial will explain multiple ways to convert a float to a string in Python. We can convert a float to a string using the str() and repr() functions.

Python Convert Float to String Using str() Function

The str() method takes an object as input and converts it into a string type. The example code below demonstrates how to use the str() function to convert a float to string in Python.

my_float = 0.125  string = str(my_float) print(string) print(type(string)) 

Output:

0.125 <class 'str'> 

Python Convert Float to String Using repr() Function

The repr() method converts an object into a printable string. The example code below demonstrates how the repr() function converts a float to a string in Python.

my_float = 0.125  string = repr(my_float) print(string) print(type(string)) 

Output:

0.125 <class 'str'> 

  • Related HOW TO?