String type conversion

本文介绍了Python中使用内置函数str()将不同类型的变量转换为字符串类型的方法,包括整型(int)、浮点型(float)、复数型(complex)和布尔型(bool)的转换。通过str()函数,可以将数值类型的数据转换为便于处理和展示的字符串形式。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

In str type conversion, we use the built-in funtion str() to convert variables of other types to a string type. This function returns the string type of object (value).

Casting int to str type

num = 11
print(type(num)) # class 'int'

# convert int to str type
s1 = str(num)
print(s1) # Output '11'
print(type(s1)) # Output class 'str'

Casting float type to str type

num = 11.58
print(type(num)) # class 'float'

# convert float to str type
s1 = str(num)
print(s1) # Output '11.58'
print(type(sl)) # Output class 'str'

Casting complex type to str type

complex_num = 11 + 2j
print(type(complex_num)) # class 'complex'

# convert complex to str type
s1 = str(complex_num)
print(s1) # Output '(11+2j)'
print(type(s1)) # class 'str'

Casting bool type to str type

b1 = True
b2 = False
print(type(b1)) # class 'bool'

# convert bool to str type
s1 = str(b1)
s2 = str(b2)

print(s1) # Output 'True'
print(s2) # Output 'False'
print(type(s1)) # class 'str'

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值