partyzuloo.blogg.se

Python split
Python split






python split

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. There is a popular StackOverflow question around this, here I am listing the most important points from the discussions that makes total sense to me.

python split

Wouldn’t below syntax be more easy to remember and use? vowelsCSV = vowels.join(",") One question arises with many python developers is why the join() function is part of String and not list. Why join() function is in String and not in List? We can use join() function to join two strings too. When we run the above program, it will produce the following output.

python split

Let’s look at a short example for joining list in python to create a string. 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. This function takes iterable as argument and List is an interable, so we can use it with List. We can use python string join() function to join a list of strings. For example, convert a list of alphabets to a comma-separated string to save in a file. Sometimes it’s useful when you have to convert list to string. Python join list means concatenating a list of strings with a specified delimiter to form a string.








Python split