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 祝福语驱散霉运的话简短