The call of str () function in Python is very detailed

description:

The str () function transforms the object into a form suitable for human reading.
Is a built-in Python function
Return value: the string format of an object

Call str () function without parameters:

The return value is an empty string, used to create an empty string or initialize a string variable


>>>str()
' '

Call str () function with parameters

Integer


>>>str(-520)  #将整数转换为字符串
'-520'
>>>str(-520)[0]   
'-' 

Floating point

str(2.4e012)  #将浮点数转换为字符串  注意浮点数的表示:e前有数字,e后最多为三位十进制整数
'2400000000000.0'  

>>> str(1.2e2)[1]  #小数点不包含
'2'
>>> str(1.2e2)[0]
'1'
>>> str(1.2e2)[2]
'0'

List


>>>List=[13,'-12,3','love']
>>>str(List)    #将列表转换为字符串
"[13, '-12.3', 'love']"   #注意是双引号哦
>>>str(List)[0]    #注意括号也被包含在内
'['
>>> str(List)[1]
'1'
>>> str(List)[2]
'3'
>>> str(List)[3]   #列表的逗号也包含
','
>>> str(List)[4]   #列表的空字符也包含
' '
>>> str(List)[5]  #列表的单引号也包含
"'"
>>> str(List)[6]
'-'
>>> str(List)[8]
'2'
>>> str(List)[9]   #列表的小数点的也包含
'.'

Tuple

>>> tuple=(13,'-12.3','love')  #元组转换为字符串
>>> str(tuple)
"(13, '-12.3', 'love')"
>>> str(tuple)[0]
'('
>>> str(tuple)[1]
'1'
>>> str(tuple)[3]
','
>>> str(tuple)[4]   #空字符也包括
' '
>>> str(tuple)[8]  
'2'
>>> str(tuple)[9]  #小数点也包括
'.'

dictionary


>>> dic={'jen':'lsh','com':'net'}   #字典转换为字符串
>>> str(dic)
"{'jen': 'lsh', 'com': 'net'}"
>>> str(dic)[6] #注意冒号也包含
':'
>>> str(dic)[7]  #注意冒号后面是空字符,冒号前面是单引号
' '
>>> str(dic)[13]
','
>>> str(dic)[14] #注意逗号后面是空字符,逗号前面是单引号
' '

set

>>> set={'A','a'}      #将集合转换为字符串
>>> str(set)
"{'a', 'A'}"    
>>> set={'ac','zc','ds'}
>>> str(set)
"{'zc', 'ds', 'ac'}"
>>> set={'love','china'}
>>> str(set)
"{'china', 'love'}"
>>> str(set)[0]
'{'
>>> str(set)[1]
"'"
>>> str(set)[7]
"'"
>>> str(set)[8]
','
>>> str(set)[9]  #逗号后面为空字符
' '

String

>>>str('love')
'love'

There will be no errors in this process, but it will cause additional time and space consumption

Published 57 original articles · Liked 54 · Visits 2349

Guess you like

Origin blog.csdn.net/September_C/article/details/105014411