1.2 KiB
1.2 KiB
title | localeTitle |
---|---|
String Join Method | 字符串连接方法 |
字符串连接方法
str.join(iterable)
方法用于连接具有指定字符串str
的iterable
所有元素。 如果iterable包含任何非字符串值,则会引发TypeError异常。
iterable
:字符串的所有迭代。可以是字符串列表,字符串元组甚至是普通字符串。
例子
1)用":"
加入字符串ist
print ":".join(["freeCodeCamp", "is", "fun"])
产量
freeCodeCamp:is:fun
2)用" and "
加入一个字符串元组
print " and ".join(["A", "B", "C"])
产量
A and B and C
3)在字符串中的每个字符后面插入一个" "
print " ".join("freeCodeCamp")
输出:
free C ode C amp
4)加入空字符串。
list1 = ['p','r','o','g','r','a','m']
print("".join(list1))
输出:
program
5)加入套装。
test = {'2', '1', '3'}
s = ', '
print(s.join(test))
输出:
2, 3, 1