How do I do this in Python 3 (string.join())?

2QdxY4RzWzUUiLuE at potatochowder.com 2QdxY4RzWzUUiLuE at potatochowder.com
Wed Aug 26 09:44:25 EDT 2020


On 2020-08-26 at 14:22:10 +0100,
Chris Green <cl at isbd.net> wrote:

> I have the following line in Python 2:-
> 
>     msgstr = string.join(popmsg[1], "\n")      # popmsg[1] is a list containing the lines of the message
> 
> ... so I changed it to:-
> 
>     s = "\n"
>     msgstr = s.join(popmsg[1])      # popmsg[1] is a list containing the lines of the message
> 
> However this still doesn't work because popmsg[1] isn't a list of
> strings, I get the error:-
> 
>     TypeError: sequence item 0: expected str instance, bytes found
> 
> So how do I do this?  I can see clumsy ways by a loop working through
> the list in popmsg[1] but surely there must be a way that's as neat
> and elegant as the Python 2 way was?

Join bytes objects with a byte object:

    b"\n".join(popmsg[1])


More information about the Python-list mailing list