Home » Python

Convert a float value to the string using str() function in Python

An example of str() function in Python: Here, we are going to learn how to convert a given float value to the string using str() function in Python?
Submitted by Anuj Singh, on August 08, 2019

Given a float value and we have to convert the value to the string using str() function.

Python code to convert a float value to the string value

# Python code to convert a float value 
# to the string value

# float value 
f_value = 1.23456

# converting to the string value
s_value = str(f_value)

# printing the float & string values with their types
print("f_value: ", f_value)
print("type(f_value): ", type(f_value))

print("s_value: ", s_value)
print("type(s_value): ", type(s_value))

# another operation by multiplying the value 
print("f_value*4: ", f_value*4)
print("s_value*4: ", s_value*4)

Output

f_value:  1.23456
type(f_value):  <class 'float'>
s_value:  1.23456
type(s_value):  <class 'str'>
f_value*4:  4.93824
s_value*4:  1.234561.234561.234561.23456

Code explanation:

In the above code f_value is a float variable contains a float value, we are converting f_value to the string by using “str() function” and storing the result in s_value variable. For further verification of the types – we are printing the types of both variables with their values and also printing their 4 times multiplied value.






Comments and Discussions

Ad: Are you a blogger? Join our Blogging forum.
Learn PCB Designing: PCB DESIGNING TUTORIAL




Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing » Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.