join()函数
join()函数:返回通过指定字符连接序列中的元素后生成的新字符串。例子一:
#coding=utf-8
Message_1 = 'zhuohua'
Message_2 = '中国'
Message_3 = 68.5
print(Message_3)
print(type(Message_3))
Message_3 = str(Message_3) #数据类型必须为字符串
print(Message_3)
print(type(Message_3))
print("-" * 10)
list_1 = [Message_1,Message_2,Message_3] #列表
print(list_1)
print(type(list_1))
print("-" * 10)
Key_1 = '' #结果不使用分隔符
Key_2 = Key_1.join(list_1)
print(Key_2)
print(type(Key_2))
print("-" * 10)
print("-" * 10)
Key_1 = ',' #结果以","作为分隔符
Key_2 = Key_1.join(list_1)
print(Key_2)
print(type(Key_2))
print("-" * 10)
Key_3 = Key_2.split(',')[2]
print(Key_3)
print(type(Key_3))
Key_3 = float(Key_3)
print(Key_3)
print(type(Key_3))
脚本运行的结果:
C:\Users\jacky\Desktop>python xx.py
68.5
<class 'float'>
68.5
<class 'str'>
----------
['zhuohua', '中国', '68.5']
<class 'list'>
----------
zhuohua中国68.5
<class 'str'>
----------
----------
zhuohua,中国,68.5
<class 'str'>
----------
68.5
<class 'str'>
68.5
<class 'float'>
C:\Users\jacky\Desktop>
######
例子二:
#coding=utf-8
Message_1 = 'zhuohua'
Message_2 = '中国'
Message_3 = '68' #数据类型必须为字符串
tuple_1 = (Message_1,Message_2,Message_3) #元组
print(tuple_1)
print(type(tuple_1))
print("-" * 10)
Key_1 = ',' #结果以","作为分隔符
Key_2 = Key_1.join(tuple_1)
print(Key_2)
print(type(Key_2))
print("-" * 10)
Key_1 = '_' #结果以"_"作为分隔符
Key_2 = Key_1.join(tuple_1)
print(Key_2)
print(type(Key_2))
print("-" * 10)
print("-" * 10)
Key_1 = ' ' #结果以一个空格作为分隔符
Key_2 = Key_1.join(tuple_1)
print(Key_2)
print(type(Key_2))
print("-" * 10)
Key_3 = Key_2.split()[2]
print(Key_3)
print(type(Key_3))
Key_3 = int(Key_3)
print(Key_3)
print(type(Key_3))
脚本运行的结果:
C:\Users\jacky\Desktop>python xx.py
('zhuohua', '中国', '68')
<class 'tuple'>
----------
zhuohua,中国,68
<class 'str'>
----------
zhuohua_中国_68
<class 'str'>
----------
----------
zhuohua 中国 68
<class 'str'>
----------
68
<class 'str'>
68
<class 'int'>
C:\Users\jacky\Desktop>
######
例子三:
#coding=utf-8
dict_1 = {'name': 'zhuohua.store\\168/', 'age': 18, 'address': 'China'} #字典
Key_1 = ',' #结果以","作为分隔符
Key_2 = Key_1.join(dict_1)
print(Key_2)
print(type(Key_2))
print("-" * 10)
print(dict_1['name'])
print(type(dict_1['name']))
print("-" * 10)
Key_1 = ',' #结果以","作为分隔符
Key_2 = Key_1.join(dict_1['name'])
print(Key_2)
print(type(Key_2))
print("-" * 10)
print(Key_2[0])
print(Key_2[1])
Key_3 = Key_2[0] + Key_2[2] + Key_2[4] + Key_2[6]
print(Key_3)
脚本运行的结果:
C:\Users\jacky\Desktop>python xx.py
name,age,address
<class 'str'>
----------
zhuohua.store\168/
<class 'str'>
----------
z,h,u,o,h,u,a,.,s,t,o,r,e,\,1,6,8,/
<class 'str'>
----------
z
,
zhuo
C:\Users\jacky\Desktop>
######
例子四:(合并路径)
#coding=utf-8
list_1 = ['/share/','check/','__pycache__/']
list_2 = ''.join(list_1)
print(list_2)
Key_1 = f"{list_2}Ping.py"
print(Key_1)
print("-" * 10)
tuple_1 = ('\\share\\','check\\','__pycache__\\')
tuple_2 = ''.join(tuple_1)
print(tuple_2)
Key_2 = f"D:{tuple_2}Ping.py"
print(Key_2)
脚本运行的结果:
C:\Users\jacky\Desktop>python xx.py
/share/check/__pycache__/
/share/check/__pycache__/Ping.py
----------
\share\check\__pycache__\
D:\share\check\__pycache__\Ping.py
C:\Users\jacky\Desktop>
相关文章:
[url=http://blog.zhuohua.store/viewthread.php?tid=133&extra=page%3D1]split()函数[/url]
[url=http://blog.zhuohua.store/viewthread.php?tid=139&page=1&extra=#pid140]字典[/url]
[url=http://blog.zhuohua.store/viewthread.php?tid=142&extra=page%3D1]Python3使用Socket发送信息[/url]
[url=http://blog.zhuohua.store/viewthread.php?tid=140&page=1&extra=#pid141]Linux使用第三方库psutil[/url]
[url=http://blog.zhuohua.store/viewthread.php?tid=141&page=1&extra=#pid142]Windows使用第三方库psutil[/url]
页:
[1]