本文共 1256 字,大约阅读时间需要 4 分钟。
join() is an in-built method in Python and it is used to join elements of the list, string etc with the given str separator.
join()是Python中的一种内置方法,用于通过给定的str分隔符连接列表,字符串等元素。
Note: Method is called with the separator and as arguments elements are provided, then the final (returned) string is a joined string by the separator string.
注意:方法是使用分隔符调用的,并提供了arguments元素,然后最后一个(返回的)字符串是分隔符字符串连接的字符串。
Syntax:
句法:
String.join(iterable)
Example 1: Joining string with hyphen ('-') character
示例1:使用连字符('-')连接字符串
# s as separator string s = "-"# str as sequence of stringsstr = ("Hello", "World", "How are?")# join and print the stringprint (s.join(str))
Output
输出量
Hello-World-How are?
Example 2: Joining string with spaces, a student detail is provided as string sequences
示例2:将字符串与空格连接,将学生详细信息作为字符串序列提供
# s as separator string (contains - space)s = " "# a student detailsfirst_name = "Amit"second_name = "Shukla"age = "21"branch = "B.Tech"# creating string sequencesstr = (first_name, second_name, age, branch)# value of str before joiningprint "str before join..."print (str)# join and print the stringprint "str after join..."print (s.join(str))
Output
输出量
str before join... ('Amit', 'Shukla', '21', 'B.Tech') str after join... Amit Shukla 21 B.Tech
翻译自:
转载地址:http://uexzd.baihongyu.com/