Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How do you concatenate with a list?

I’m relatively new to python and I’m trying to concatenate with lists. This is my best attempt but obviously it doesn’t work. Anyone know a way to do this? Thanks

present = ["Jake", "Jimmy", "Karen"]
print("The people present are " + present)

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

you need to convert the list into a string before you can do the string concatenation with the + operator. You can use the str.join() method to accomplish this.

present = ["Jake", "Jimmy", "Karen"]
print("The people present are " + ", ".join(present))

This is going to create a string with each item in the list and use the string ', ' as a separator between each element.

Check out the w3 schools examples here https://www.w3schools.com/python/ref_string_join.asp

Or the official python docs https://docs.python.org/3/library/stdtypes.html#str.join

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading