[Python] Remove the trailing comma when format string with tuples

Discussão em 'Python' iniciado por Stack, Junho 27, 2025.

  1. Stack

    Stack Membro Participativo

    I'm doing string formatting with tuples:

    a = (1,2,3)
    s = f"something in {a}"
    print(s)


    'something in (1, 2, 3)'


    Everything is fine until I encounter a single-element tuple, which gives:

    a = (1,)
    s = f"something in {a}"


    'something in (1,)'


    what I actually want is:

    'something in (1)'


    How do I make tuple string formatting behaves consistently and remove the trailing comma?

    Continue reading...

Compartilhe esta Página