[Tutor] concat vs join - followup

Alan Gauld alan.gauld at blueyonder.co.uk
Sun Aug 29 12:35:53 CEST 2004

> Why do you prefer this way? It seems kind of indirect to me, and
much more
> verbose than join(), for example why do you prefer this
> >fmt = "%s" * len(strings)
> >result = fmt % strings
> to this?
> result = ''.join(strings)

Three reasons:
1) I'm originally a C programmer and that's how I would do it
   in C - using sscanf (I rarely use strcat/strncat because of
   the huge bug potential).
2) Habit. The string methods weren't there when I learned Python
   and string.join() meant importing a module and more typing.
3) It worked and has always been "fast enough" for my fairly
   modest needs

To be honest I very rarely find a need to simply join a lot of
strings together, I'm much more likely to be adding some extra
characters around the joins, inserting variable values etc.
So formatting is more flexible from that point of view. If
I really were simply joining a list of strings together I'm
not sure what I would really use in the white heat of a
coding session - maybe even reduce() - which is probably the
worst performing solution of all!! :-)

Alan g.

More information about the Tutor mailing list