"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