Skip to main content

Python: Convert List to String

The method keyword is similar like in JavaScript. Both are using the join keyword. But in Python, the syntax is reversed (or not, depends on your perspective).

JavaScript

For example:

In the example above, the array input is converted to strings output with one horizontal white space as the separator.


Python

For example:

In the example above, the list input is converted to strings output also with one horizontal white space as the separator.

Unlike in JavaScript, which we can convert an array with nonuniform items to string, the join() method in Python only works for list which contains all string (such as the example above).

To join those mixed items, first we need to convert each element to string using str(). Then apply join().

Let's use and expand the input:

The method above won't change the original input list.

We can also separate the iteration and conversion for our amusement, then make a function definition for it. Let's name the function as convertToString. Here goes:

This method also won't change the original list. As you can see, there's that clonedList variable.

PLATYY
Last modified on

Comments

Monkey Raptor uses cookies or biscuits 🍪 for analytics, functionality, and advertisements. More info in Privacy Policy