sprintf like functionality in Python

后端 未结 11 1876
暖寄归人
暖寄归人 2020-12-23 02:50

I would like to create a string buffer to do lots of processing, format and finally write the buffer in a text file using a C-style sprintf functionality in Pyt

11条回答
  •  天命终不由人
    2020-12-23 02:59

    If I understand your question correctly, format() is what you are looking for, along with its mini-language.

    Silly example for python 2.7 and up:

    >>> print "{} ...\r\n {}!".format("Hello", "world")
    Hello ...
     world!
    

    For earlier python versions: (tested with 2.6.2)

    >>> print "{0} ...\r\n {1}!".format("Hello", "world")
    Hello ...
     world!
    

提交回复
热议问题