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

python字符串转换浮点

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

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

Python code to convert a float value to the string value

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

# 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.

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

Recommended posts

推荐的帖子

翻译自: https://www.includehelp.com/python/convert-a-float-value-to-the-string-using-str-function-in-python.aspx

python字符串转换浮点

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值