Using multiple arguments for string formatting in Python (e.g., '%s … %s')

后端 未结 8 2124
谎友^
谎友^ 2020-12-04 08:16

I have a string that looks like \'%s in %s\' and I want to know how to seperate the arguments so that they are two different %s. My mind coming from Java came u

8条回答
  •  旧巷少年郎
    2020-12-04 09:04

    Mark Cidade's answer is right - you need to supply a tuple.

    However from Python 2.6 onwards you can use format instead of %:

    '{0} in {1}'.format(unicode(self.author,'utf-8'),  unicode(self.publication,'utf-8'))
    

    Usage of % for formatting strings is no longer encouraged.

    This method of string formatting is the new standard in Python 3.0, and should be preferred to the % formatting described in String Formatting Operations in new code.

提交回复
热议问题