添加链接
link管理
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接

// Tutorial //

Python Join List

Published on August 3, 2022
Default avatar

By Pankaj

Python Join List

While we believe that this content benefits our community, we have not yet thoroughly reviewed it. If you have any suggestions for improvements, please let us know by clicking the “report an issue“ button at the bottom of the tutorial.

Python join list means concatenating a list of strings with a specified delimiter to form a string. Sometimes it’s useful when you have to convert list to string. For example, convert a list of alphabets to a comma-separated string to save in a file.

Python Join List

python string join() function to join a list of strings. This function takes iterable as argument and List is an interable, so we can use it with List. Also, the list should contain strings, if you will try to join a list of ints then you will get an error message as TypeError: sequence item 0: expected str instance, int found . Let’s look at a short example for joining list in python to create a string.

vowels = ["a", "e", "i", "o", "u"]
vowelsCSV = ",".join(vowels)
print("Vowels are = ", vowelsCSV)

When we run the above program, it will produce the following output.

Vowels are =  a,e,i,o,u

Python join two strings

Why join() function is in String and not in List?

StackOverflow question around this, here I am listing the most important points from the discussions that makes total sense to me.

The main reason is that join() function can be used with any iterable and result is always a String, so it makes sense to have this function in String API rather than having it in all the iterable classes.

Joining list of multiple data-types

Split String using join function

Using split() function

Splitting only n times