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

Facing issues? Have Questions? Post them here! I am happy to answer!

Author Info:

Rakesh (He/Him) has over 14+ years of experience in Web and Application development. He is the author of insightful How-To articles for Code2care.

Follow him on: X

You can also reach out to him via e-mail: rakesh@code2care.org

Copyright © Code2care 2024 | Privacy Policy | About Us | Contact Us | Sitemap