Which of the following functions converts a list into a string?

4 Ways to Convert List to String Python: Join, Traversal & More

By SimplilearnLast updated on Dec 8, 202114383266
Which of the following functions converts a list into a string?

Table of Contents

View More

Python is one of the most popular programming languages today, and in this tutorial we will learn various nuances of Python, including what is a list in python, what is a string, ways to change a list to a string and more. Let’s start.

What Is a List in Python?

A list in python is an ordered sequence that can hold a variety of object types, such as, integer, character or float. A list in python is equivalent to an array in other programming languages. It is represented using square brackets, and a comma(,) is used to separate two objects present in the list.

Python Training Course

Learn Data Operations in PythonExplore Course
Which of the following functions converts a list into a string?

A list and an array in other programming languages differ in the way that an array only stores a similar data type, meaning that an array is homogeneous in nature, but a list in python can store different data types at a time, and therefore it can either be homogeneous or heterogeneous. Below are some examples of homogeneous and heterogeneous lists in python:

Homogenous Lists:

Which of the following functions converts a list into a string?

Heterogeneous Lists:

Which of the following functions converts a list into a string?

Accessing an Item From the List

Which of the following functions converts a list into a string?

An item from the list can be accessed by referring to its index in the list. The indexing of elements in the list starts from 0. Let’s take an example of the list we created in the last step.

To access an element from the list, we pass the index of that element in the print function.

As mentioned earlier, that indexing starts from 0, so when index [1] is passed, it gives the result as “dog”. Similarly, if we pass the index, say [2], it will give the output 2.2

Free Course: Python for Beginners

Master the fundamentals of PythonEnroll Now
Which of the following functions converts a list into a string?

Various methods to convert lists to string in python?

In this short tutorial, we look at all the different methods and the code that can be used to convert list to string in Python.

Conversion of List of characters to a string

Even a set of characters in the form of a list can be converted to a string in the same manner as above. Here is an example to demonstrate the conversion of a list of characters to a string.

Example:

inp_str = ['J', 'o', 'u', 'r', 'n', 'a', 'l', 'd', 'e', 'v'] st = "" for x in inp_str: st += x print(st)

Output:

Journaldev

Conclusion

Thus, in this article, we have studied the different techniques and methods of conversion of Python List to String.


References

  • StackOverflow – Conversion of List to a String in Python

Python program to convert a list to string

Given a list, write a Python program to convert the given list to string.

There are various situation we might encounter when a list is given and we convert it to string. For example, conversion to string from the list of string or the list of integer.

Example:

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

Let’s see various ways we can convert the list to string.

Method #1:
Iterate through the list and keep adding the element for every index in some empty string.






# 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 = ['Geeks', 'for', 'Geeks']
print(listToString(s))
Output: GeeksforGeeks

Method #2: Using .join() method




# 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 = ['Geeks', 'for', 'Geeks']
print(listToString(s))
Output: Geeks for Geeks

But what if the list contains both string and integer as its element. In those cases, above code won’t work. We need to convert it to string while adding to string.

Method #3: Using list comprehension




# 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

Method #4: Using map()
Use map() method for mapping str (for converting elements in list to string) with given iterator, the list.




# 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

Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.

To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course. And to begin with your Machine Learning Journey, join the Machine Learning - Basic Level Course




Article Tags :
Python
Python Programs
Python list-programs
Python string-programs
python-list
python-string
Practice Tags :
python-list

Python | Program to convert String to a List

In this program, we will try to convert a given string to a list, where spaces or any other special characters, according to the users choice, are encountered. To do this we use the split() method.
Syntax:

string.split("delimiter")

Examples:

Input : "Geeks for Geeks" Output : ['Geeks', 'for', 'Geeks'] Input : "Geeks-for-Geeks" Output : ['Geeks', 'for', 'Geeks']

The split method is used to split the strings and store them in the list. The built-in method returns a list of the words in the string, using the “delimiter” as the delimiter string. If a delimiter is not specified or is None, a different splitting algorithm is applied: runs of consecutive whitespace are regarded as a single separator, and the result will contain no empty strings at the start or end if the string has leading or trailing whitespace.
Example 1:




# Python code to convert string to list
def Convert(string):
li = list(string.split(" "))
return li
# Driver code
str1 = "Geeks for Geeks"
print(Convert(str1))

Output:

['Geeks', 'for', 'Geeks']

Example 2:






# Python code to convert string to list
def Convert(string):
li = list(string.split("-"))
return li
# Driver code
str1 = "Geeks-for-Geeks"
print(Convert(str1))

Output:

['Geeks', 'for', 'Geeks']

Example 3:




# Python code to convert string to list character-wise
def Convert(string):
list1=[]
list1[:0]=string
return list1
# Driver code
str1="ABCD"
print(Convert(str1))

Output:

['A','B','C','D']

Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.

To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course. And to begin with your Machine Learning Journey, join the Machine Learning - Basic Level Course




Article Tags :
Python
Python list-programs
Python string-programs
python-list
python-string
Practice Tags :
python-list

4 Ways to Convert List to String in Python

Which of the following functions converts a list into a string?

While working with python data types, there are situations when you want to convert the data collected from one type to another. There are 4 methods by which we can convert a list to a string in python with fewer lines of code. These methods of converting a list into a string include iteration, comprehension, join(), and map() method. But before understanding all these conversion methods in detail, let us understand what are lists and strings.