当前位置: 首页 > 图灵资讯 > 行业资讯> python中str()函数转换字符串

python中str()函数转换字符串

发布时间:2024-08-08 16:03:56

1、方法说明

如果只是想把Python的对象转换成文字串,str()函数是回到人类可读值的表示。

2、语法

classstr(object='')

3、实例

>>>s='Hello,world.'
>>>str(s)
'Hello,world.'
>>>repr(s)
"'Hello,world.'"
>>>str(1/7)
'0.14285714285714285'
>>>x=10*3.25
>>>y=200*200
>>>s='Thevalueofxis'+repr(x)+',andyis'+repr(y)+'...'
>>>print(s)
Thevalueofxis32.5,andyis4000000...
>>>#Therepr()ofastringaddsstringquotesandbackslashes:
...hello='hello,world\n'
>>>hellos=repr(hello)
>>>print(hellos)
'hello,world\n'
>>>#Theargumenttorepr()maybeanyPythonobject:
...repr((x,y,('spam','eggs')))
"(32.5,40000,'spam','eggs'))"

以上是python中str()函数转换字符串的方法,希望对大家有所帮助。更多Python学习指导:基础教程python基础教程

相关文章

如何让vim支持python3

如何让vim支持python3

2025-09-12
python2.7和3.6区别有哪些

python2.7和3.6区别有哪些

2025-09-12
python3有serial库吗

python3有serial库吗

2025-09-12
python中w、r表示什么意思

python中w、r表示什么意思

2025-09-12
python中如何把list变成字符串

python中如何把list变成字符串

2025-09-12
python命名空间是什么

python命名空间是什么

2025-09-12