String formatting: % vs. .format vs. string literal

后端 未结 16 3298
青春惊慌失措
青春惊慌失措 2020-11-21 04:18

Python 2.6 introduced the str.format() method with a slightly different syntax from the existing % operator. Which is better and for what situations?

Pyt

16条回答
  •  生来不讨喜
    2020-11-21 04:42

    For python version >= 3.6 (see PEP 498)

    s1='albha'
    s2='beta'
    
    f'{s1}{s2:>10}'
    
    #output
    'albha      beta'
    

提交回复
热议问题