[Python] Python .join or string concatenation

Discussão em 'Python' iniciado por Stack, Março 7, 2025.

  1. Stack

    Stack Membro Participativo

    I realise that if you have an iterable you should always use .join(iterable) instead of for x in y: str += x. But if there's only a fixed number of variables that aren't already in an iterable, is using .join() still the recommended way?

    For example I have

    user = 'username'
    host = 'host'


    should I do

    ret = user + '@' + host


    or

    ret = '@'.join([user, host])


    I'm not so much asking from a performance point of view, since both will be pretty trivial. But I've read people on here say always use .join() and I was wondering if there's any particular reason for that or if it's just generally a good idea to use .join().

    Continue reading...

Compartilhe esta Página