"sep".join(iterable)
join用于以指定分隔符将可疊代對象【成員必須為str類型】連接配接為一個新的字元串,分隔符可以為空
傳回值位字元串
os.path.join(path,path1,...)
傳回多個路徑拼接後的路徑【第一個絕對路徑之前的參數被忽略】
示例:
import os
string = "test"
lis = ['w', 'e', 'q']
tpl = ('w', 'e', 'q')
dic = {"55": 5, "44": 4, "22": 2, "33": 3, "11": 1}
print("11".join(string)) # a = "11".join(string) print(a) print(type(a))
print("".join(tpl))
print(" ".join(lis))
print("key is : [%s] " % (",".join(dic)))
# 字元串去重并按從大到小排列
words = "wsasdeddcewtttwssa"
words_set = set(words) # 集合特性實作去重 字元串集合化
words_list = list(words_set) # 集合清單化
words_list.sort(reverse=True) # 設定排序為從大到小
new_words = "".join(words_list) # join方法以空位分隔符拼接清單元素位新字元串
print(words_list)
print(new_words)
print(os.path.join("/home/", "test/", "python")) # 【傳回/home/test/python】
print(os.path.join("/home", "/test", "/python"), end="") # 【傳回/python,前兩個參數被忽略】
結果:
D:\mypy\venv\Scripts\python.exe D:/mypy/join.py
t11e11s11t
weq
w e q
key is : [55,44,22,33,11]
['w', 't', 's', 'e', 'd', 'c', 'a']
wtsedca
/home/test/python
/python
Process finished with exit code 0
标簽:join,函數,python,list,test,words,print