pythonstr函数为什么不能用
str函数是Python的内置函数,它将参数转换成字符串类型,即人适合阅读的形式。
其语法格式为
str(object)
返回值:
返回object的字符串形式
使用示例
1.无参调用
当str()函数的参数都省略时,函数返回空字符串。这种情况常用来创建空字符串或者初始化字符串变量。
>>>str()
''
2.不省略参数
str函数将整数、浮点数、列表、元组、字典和集合转换为字符串类型
>>>str(-23)#整数转换为字符串
'-23'
>>>str(1.3e2)#浮点数转换为字符串
'130.0'
>>>a_list=[12,'-23.1','Python']
>>>str(a_list)#列表转换为字符串
"[12,'-23.1','Python']"
>>>str(a_list)[0]
'['
>>>a_tuple=(23,'9we',-8.5)
>>>str(a_tuple)#元组转换为字符串
"(23,'9we',-8.5)"
>>>a_dictionary={'Huawei':'China','Apple':'USA'}
>>>str(a_dictionary)#字典转换为字符串
"{'Huawei':'China','Apple':'USA'}"
>>>str(a_dictionary)[10]
''
>>>a_set={'China','Japan','UK'}
>>>str(a_set)#集合转换为字符串
"{'Japan','UK','China'}"
注意
将列表、元组、字典和集合转换为字符串后,包裹列表、元组、字典和集合的'['、']'、'('、')'、'{'、'}',以及列表、元组、
字典和集合中的元素分隔符',',和字典中键值对':'也都转换成了字符串,是转换后字符串的一部分。
以上内容为大家介绍了python培训之str函数为什么不能用,希望对大家有所帮助,如果想要了解更多Python相关知识,请关注IT培训机构:千锋教育。

相关推荐HOT
更多>>
python函数标注是什么
python函数标注是什么在参数的使用方法中,还有一种解包的情况是需要我们掌握的。比如,将列表或者字典的值转换为函数的参数,就需要用到参数解...详情>>
2023-11-14 14:13:35
python正负索引的使用
python正负索引的使用本文教程操作环境:windows7系统、Python3.9.1,DELLG3电脑。1、说明使用正索引(从左至右)、负索引(从右至左)访问元素,时...详情>>
2023-11-14 13:45:04
python垃圾回收的机制过程
python垃圾回收的机制过程本文教程操作环境:windows7系统、Python3.9.1,DELLG3电脑。1、步骤(1)收集所有容器对象(list,dict,tuple,customClas...详情>>
2023-11-14 13:05:15
pythontry语句的执行原理
pythontry语句的执行原理1、原理分析(1)执行try子句(try和except关键字之间的多行语句)。(2)如果没有异常发生,则跳过except子句并完成try语句...详情>>
2023-11-14 09:37:35热门推荐
技术干货






