Python

Converting List to String in Python

Converting List to String in Python

Python is one of the most popular programming languages today, and in this lesson, we will learn how to convert a list to a string in a variety of methods. Aside from that, we will go through Python details such as what is a list in Python, what is a string, and so on. Let us get started.

List in Python

In Python, a list is an ordered sequence that can hold several object types such as integer, character, or float. In various computer languages, a list is equivalent to an array. Square brackets are used to denote it, and a comma (,) is used to divide two things in the list.

A list and an array in other programming languages differ in that an array only stores the same data type, indicating that it is homogenous, whereas a list in Python can store diverse data types at the same time, indicating that it can be either homogeneous or heterogeneous. Here are some Python examples of homogeneous and heterogeneous lists:

Homogeneous List

Input: ['Board', 'Infinity']

Output: Board Infinity

Input: ['I', 'want', 4, 'apples', 'and', 18, 'bananas']

Output: I want 4 apples and 18 bananas

String in Python

In Python, a string is an ordered sequence of characters. It should be emphasized that a list is an ordered sequence of object types, whereas a string is an ordered sequence of characters. This is the primary distinction between the two.

A sequence is a data type made up of several components of the same type, such as integers, floats, characters, and so on. This means that a string is a subset of the sequence data type, with all items represented as characters.

# Python program to convert a list to string

# Function to convert

def listToString(s):

# initialize an empty string

str1 = ""

# traverse in the string

for ele in s:

str1 += ele

# return string

return str1

# Driver code

s = ['Board', 'Infinity']

print(listToString(s))

write your code here: Coding Playground

Output

BoardInfinity

We declare a string by assigning it to a variable. In this case, ‘a’ is a variable associated with the string Board Infinity. A string element can be accessed in the same way that a list element can. The indexing of elements in a string begins at 0.

How to Convert a List to String in Python

Using Join Function

The join function in Python is one of the simplest ways to convert a list to a string. The essential thing to remember when using this method is that the join function can only convert lists that contain just strings as elements into strings.

# Python program to convert a list

# to string using join() function

# Function to convert

def listToString(s):

# initialize an empty string

str1 = " "

# return string

return (str1.join(s))

# Driver code

s = ['Borad', 'Infinity']

print(listToString(s))

write your code here: Coding Playground

Output

BoardInfinity

Using Map Function

The map function can be used to convert a list to a string in two ways.

  • if the list just contains numbers
  • If the list is diverse,

The map() method takes two arguments;

The str() method converts the provided data type to the string data type.

An iterable sequence; the str() function will be called on each element in the sequence. An iterator will be used to return the string values.

Finally, the join() function is used to combine all of the values returned by str().

# Python program to convert a list

# to string using list comprehension

s = ['I', 'want', 4, 'apples', 'and', 18, 'bananas']

# using list comprehension

listToStr = ' '.join(map(str, s))

print(listToStr)

Output:

I want 4 apples and 18 bananas

List Comprehension

In Python, list comprehension builds a list of elements from an existing list. The for loop is then used to traverse the iterable objects in an element-wise manner.

Use Python List Comprehension with the join() method to convert a list to a string. The list comprehension will iteratively traverse the elements, and the join() method will concatenate the elements of the list into a new string and return it as output.

# Python program to convert a list

# to string using list comprehension

s = ['I', 'want', 4, 'apples', 'and', 18, 'bananas']

# using list comprehension

listToStr = ' '.join([str(elem) for elem in s])

print(listToStr)

Output

I want 4 apples and 18 bananas

Conclusion

This brings us to the end of the article. These are the various techniques to convert list to string using python.