1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.
  2. Anuncie Aqui
    Anuncie aqui você Também: fdantas@4each.com.br

[Python] python: have a default value when formatting strings (str.format() method)

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

  1. Stack

    Stack Membro Participativo

    Is there a way to have a default value when doing string formatting? For example:

    s = "Text {0} here, and text {1} there"
    s.format('foo', 'bar')


    What I'm looking for is setting a default value for a numbered index, so that it can be skipped in the placeholder, e.g. something like this:

    s = "Text {0:'default text'} here, and text {1} there"


    I checked https://docs.python.org/3/tutorial/inputoutput.html#tut-f-strings and didn't find what I need, maybe looking in the wrong place?

    Thanks.

    Continue reading...

Compartilhe esta Página