Python - Convert float to String


Example 1: Using str() method

my_float=1234.56 #Float

float_to_string=str(my_float)

print(type(my_float))
print(type(float_to_string))

print(float_to_string)
Output:
<class 'float'>
<class 'str'>
1234.56
Float to String Python Example


Example 2: using format() method

my_float=1234.56 #Float

float_to_string=format(my_float,'f')

print(type(my_float))
print(type(float_to_string))

print(float_to_string)
Output:
<class 'float'>
<class 'str'>
1234.560000
Python Float to String using format


Example 3: Using format string

my_float=1234.5678

float_to_string=f"{my_float:.4f}"

print(type(my_float))
print(type(float_to_string))
print(float_to_string)
Output:
<class 'float'>
<class 'str'>
1234.5678
Python float to string using string formatting


Have Questions? Post them here!
Advertisements
Try Out Code2care Dev Tools:

Advertisements

Advertisements
Code2care is an initiative to publish and share varied knowledge in programming and technical areas gathered during day-to-day learnings and development activities.

Students and software developers can leverage this portal to find solutions to their various queries without re-inventing the wheel by referring to our easy to understand posts. Technical posts might include learnings, tutorials, trouble-shooting steps, video tutorials, code snippets, how-to, blogs, articles, etc.

🎉 We are celebrating the 10th years of Code2care! Thank you for all your support!

We strongly support Gender Equality & Diversity.