[Tutor] joining strings using arrays and string variables.

Alan Gauld alan.gauld at yahoo.co.uk
Sun Feb 7 07:25:14 EST 2021


On 07/02/2021 11:06, mhysnm1964 at gmail.com wrote:

>>>> a = ['hello','fred','and','tom']
> 
>>>> b = '/'.join(['welcome', 'the', 'following', a])
> 
> 
> Traceback (most recent call last):
> TypeError: sequence item 3: expected str instance, list found

The problem is that a is a list. You need to flatten the two
lists into a single list.

+ will do that
>>> [1,2,3]+[4,5,6]
[1,2,3,4,5,6]

So in your case:

b = '/'.join(['welcome', 'the', 'following'] + a)


-- 
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.amazon.com/author/alan_gauld
Follow my photo-blog on Flickr at:
http://www.flickr.com/photos/alangauldphotos




More information about the Tutor mailing list