0

How do i convert a list into a string in python?

I have tried using a for loop but i want a simpler method to join.

Input:

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

Output:

I want 4 apples and 18 bananas

2 Answers 2

1

This is not the best method, but since you don't want to use a for loop:

>>> ("{} " * len(my_list)).format(*my_list).strip()
'I want 4 apples and 18 bananas'
Sign up to request clarification or add additional context in comments.

Comments

0

" ".join([str(x) for x in ['I', 'want', 4, 'apples', 'and', 18, 'bananas']]) would work for your example.

More generally, " ".join(str(x) for x in iterable) works.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.