python里的join函数_python中join函数

"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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值