Python string formatting when string contains “%s” without escaping

后端 未结 4 1305

When formatting a string, my string may contain a modulo \"%\" that I do not wish to have converted. I can escape the string and change each \"%\"

4条回答
  •  难免孤独
    2020-12-31 03:57

    You could (and should) use the new string .format() method (if you have Python 2.6 or higher) instead:

    "Day old bread, 50% sale {0}".format("today")
    

    The manual can be found here.

    The docs also say that the old % formatting will eventually be removed from the language, although that will surely take some time. The new formatting methods are way more powerful, so that's a Good Thing.

提交回复
热议问题