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

后端 未结 4 1310

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:51

    Escaping a '%' as '%%' is not a workaround. If you use String formatting that is the way to represent a '%' sign. If you don't want that, you can always do something like:

    print "Day old bread, 50% sale " + "today"
    

    e.g. not using formatting.

    Please note that when using string concatenation, be sure that the variable is a string (and not e.g. None) or use str(varName). Otherwise you get something like 'Can't concatenate str and NoneType'.

提交回复
热议问题