Python字符串| join()方法与示例
join()是Python中的一种内置方法,用于通过给定的str分隔符连接列表,字符串等元素。
注意:方法是使用分隔符调用的,并提供了arguments元素,然后最后一个(返回的)字符串是分隔符字符串连接的字符串。
语法:
String.join(iterable)
示例1:使用连字符('-')连接字符串
#s作为分隔符字符串
s = "-"
#str作为字符串序列
str = ("Hello", "World", "How are?")
#连接并打印字符串
print (s.join(str))输出结果
Hello-World-How are?
示例2:用空格连接字符串,将学生详细信息作为字符串序列提供
#s作为分隔符字符串 (contains - space) s = " " #学生资料 first_name = "Amit" second_name = "Shukla" age = "21" branch = "B.Tech" #创建字符串序列 str = (first_name, second_name, age, branch) #加入前str的值 print "str before join..." print (str) #连接并打印字符串 print "str after join..." print (s.join(str))
输出结果
str before join...
('Amit', 'Shukla', '21', 'B.Tech')
str after join...
Amit Shukla 21 B.Tech热门推荐
10 祝女儿简短祝福语大全
11 大学新年祝福语简短创意
12 元旦适合的祝福语简短
13 朋友出远门祝福语简短
14 初六简短的祝福语
15 祝男孩生日祝福语简短
16 同事调离的祝福语简短
17 拜年红包的祝福语简短
18 妈妈生日祝福语简短励志