sprintf like functionality in Python

后端 未结 11 1899
暖寄归人
暖寄归人 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 03:12

    To insert into a very long string it is nice to use names for the different arguments, instead of hoping they are in the right positions. This also makes it easier to replace multiple recurrences.

    >>> 'Coordinates: {latitude}, {longitude}'.format(latitude='37.24N', longitude='-115.81W')
    'Coordinates: 37.24N, -115.81W'
    

    Taken from Format examples, where all the other Format-related answers are also shown.

提交回复
热议问题