Python字符串| join()方法与示例
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中的一種內(nèi)置方法,用于通過給定的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.
注意:方法是使用分隔符調(diào)用的,并提供了arguments元素,然后最后一個(gè)(返回的)字符串是分隔符字符串連接的字符串。
Syntax:
句法:
String.join(iterable)Example 1: Joining string with hyphen ('-') character
示例1:使用連字符('-')連接字符串
# s as separator string s = "-" # str as sequence of strings str = ("Hello", "World", "How are?")# join and print the string print (s.join(str))Output
輸出量
Hello-World-How are?Example 2: Joining string with spaces, a student detail is provided as string sequences
示例2:將字符串與空格連接,將學(xué)生詳細(xì)信息作為字符串序列提供
# s as separator string (contains - space) s = " "# a student details first_name = "Amit" second_name = "Shukla" age = "21" branch = "B.Tech"# creating string sequences str = (first_name, second_name, age, branch)# value of str before joining print "str before join..." print (str)# join and print the string print "str after join..." print (s.join(str))Output
輸出量
str before join...('Amit', 'Shukla', '21', 'B.Tech')str after join...Amit Shukla 21 B.Tech翻譯自: https://www.includehelp.com/python/string-join-method-with-example.aspx
總結(jié)
以上是生活随笔為你收集整理的Python字符串| join()方法与示例的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: C ++ STL中的set :: upp
- 下一篇: 汽车首付多少钱啊?