w3resource

Python: Concatenate elements of a list

Python List: Exercise - 54 with Solution

Write a Python program to concatenate elements of a list.

Python: Concatenate elements of a list

Sample Solution:

Python Code:

# Define a list 'color' containing color names as strings
color = ['red', 'green', 'orange']

# Use the 'join' method to concatenate the elements of the 'color' list with a hyphen '-' in between
# Print the resulting string
print('-'.join(color))

# Use the 'join' method to concatenate the elements of the 'color' list without any separator
# Print the resulting string
print(''.join(color)) 

Sample Output:

red-green-orange   
redgreenorange 

Flowchart:

Flowchart: Concatenate elements of a list

Python Code Editor:

Previous: Write a Python program to create a list with infinite elements.
Next: Write a Python program to remove key values pairs from a list of dictionaries.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Become a Patron!

Follow us on Facebook and Twitter for latest update.

It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.

https://198.211.115.131/python-exercises/list/python-data-type-list-exercise-54.php