Python program for converting a list to a string

| | |

There are various situations that we can run into when given a list and we convert it to a string. For example, converting to a string from a list of strings or a list of integers.

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 let’s see how we can convert a list to a string.

Method # 1:
Iterate through the list and keep adding an element for each index to some empty string.

Exit :

 Python.Engineering 

Method # 2: Using the .join () method

# Python program to convert list to string


# Function to convert

def listToString (s) :

# initialize p empty string

str1 = ""

# line move

for ele in s:

str1 + = ele

# returned string

return str1


# Driver code

s = [ ’Geeks’ , ’ for’ , ’Geeks’ ]

print (listToString (s))

# Python program to convert the list
# to a string using the join () function


# Function to convert

def listToString ( s):

# initialize empty string

str1 = ""

# returned string

return (str1.join (s))


# Driver code

s = [ ’Geeks’ , ’for’ , ’Geeks’ ]

print (listToString (s))

Exit :

 Geeks for Geeks 

But what if the list contains a string and an integer as an element. In these cases, the above code will not work. We need to convert it to a string when added to a string.

Method # 3: Using the comprehension list

# Python program to convert a list
# to a string using a list comprehension

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


# using comprehension list

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

print (listToStr)

Output:

 I want 4 apples and 18 bananas 

Method # 4: Using the card ()
Use the method map () to display str (to convert elements in a list to a string) with a given iterator, list.

# Python program for converting cn claim
# to a line using a list comprehension

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


# using comprehension list

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

print (listToStr)

Exit :

 I want 4 apples and 18 bananas 

Python program for converting a list to a string join: Questions

Why is it string.join(list) instead of list.join(string)?

5 answers

Evan Fosmark By Evan Fosmark

This has always confused me. It seems like this would be nicer:

my_list = ["Hello", "world"]
print(my_list.join("-"))
# Produce: "Hello-world"

Than this:

my_list = ["Hello", "world"]
print("-".join(my_list))
# Produce: "Hello-world"

Is there a specific reason it is like this?

1906

Answer #1

It"s because any iterable can be joined (e.g, list, tuple, dict, set), but its contents and the "joiner" must be strings.

For example:

"_".join(["welcome", "to", "stack", "overflow"])
"_".join(("welcome", "to", "stack", "overflow"))
"welcome_to_stack_overflow"

Using something other than strings will raise the following error:

TypeError: sequence item 0: expected str instance, int found

1906

Answer #2

This was discussed in the String methods... finally thread in the Python-Dev achive, and was accepted by Guido. This thread began in Jun 1999, and str.join was included in Python 1.6 which was released in Sep 2000 (and supported Unicode). Python 2.0 (supported str methods including join) was released in Oct 2000.

  • There were four options proposed in this thread:
    • str.join(seq)
    • seq.join(str)
    • seq.reduce(str)
    • join as a built-in function
  • Guido wanted to support not only lists and tuples, but all sequences/iterables.
  • seq.reduce(str) is difficult for newcomers.
  • seq.join(str) introduces unexpected dependency from sequences to str/unicode.
  • join() as a built-in function would support only specific data types. So using a built-in namespace is not good. If join() supports many datatypes, creating an optimized implementation would be difficult, if implemented using the __add__ method then it would ve O(n¬≤).
  • The separator string (sep) should not be omitted. Explicit is better than implicit.

Here are some additional thoughts (my own, and my friend"s):

  • Unicode support was coming, but it was not final. At that time UTF-8 was the most likely about to replace UCS2/4. To calculate total buffer length of UTF-8 strings it needs to know character coding rule.
  • At that time, Python had already decided on a common sequence interface rule where a user could create a sequence-like (iterable) class. But Python didn"t support extending built-in types until 2.2. At that time it was difficult to provide basic iterable class (which is mentioned in another comment).

Guido"s decision is recorded in a historical mail, deciding on str.join(seq):

Funny, but it does seem right! Barry, go for it...
Guido van Rossum

1906

Answer #3

Because the join() method is in the string class, instead of the list class?

I agree it looks funny.

See http://www.faqs.org/docs/diveintopython/odbchelper_join.html:

Historical note. When I first learned Python, I expected join to be a method of a list, which would take the delimiter as an argument. Lots of people feel the same way, and there’s a story behind the join method. Prior to Python 1.6, strings didn’t have all these useful methods. There was a separate string module which contained all the string functions; each function took a string as its first argument. The functions were deemed important enough to put onto the strings themselves, which made sense for functions like lower, upper, and split. But many hard-core Python programmers objected to the new join method, arguing that it should be a method of the list instead, or that it shouldn’t move at all but simply stay a part of the old string module (which still has lots of useful stuff in it). I use the new join method exclusively, but you will see code written either way, and if it really bothers you, you can use the old string.join function instead.

--- Mark Pilgrim, Dive into Python

Python program for converting a list to a string Python functions: Questions

Shop

Best laptop for Fortnite

$

Best laptop for Excel

$

Best laptop for Solidworks

$

Best laptop for Roblox

$

Best computer for crypto mining

$

Best laptop for Sims 4

$

Best laptop for Zoom

$499

Best laptop for Minecraft

$590

Latest questions

NUMPYNUMPY

psycopg2: insert multiple rows with one query

12 answers

NUMPYNUMPY

How to convert Nonetype to int or string?

12 answers

NUMPYNUMPY

How to specify multiple return types using type-hints

12 answers

NUMPYNUMPY

Javascript Error: IPython is not defined in JupyterLab

12 answers

News

Wiki

Python OpenCV | cv2.putText () method

numpy.arctan2 () in Python

Python | os.path.realpath () method

Python OpenCV | cv2.circle () method

Python OpenCV cv2.cvtColor () method

Python - Move item to the end of the list

time.perf_counter () function in Python

Check if one list is a subset of another in Python

Python os.path.join () method