使用Python中的str()函数将浮点值转换为字符串

给定一个float值,我们必须使用将值转换为字符串 str() 功能。

Python代码将浮点值转换为字符串值

# Python代码转换浮点值 
# 到字符串值

# 浮动价值 
f_value = 1.23456

# 转换为字符串值
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))

# 通过乘以另一个运算 
print("f_value*4: ", f_value*4)
print("s_value*4: ", s_value*4)

输出结果

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

代码说明:

在上面的代码中,f_value是一个包含浮点值的float变量,我们通过使用“str()函数”并将结果存储在s_value变量中。为了进一步验证类型,我们将同时打印两个变量的类型及其值,并打印其四倍的乘积值。

初步格式