Python placeholders in strings containing the % symbol

前端 未结 1 1912
自闭症患者
自闭症患者 2020-12-12 05:42

I want to be able to use a placeholder in a string that already contains a % symbol. For instance, I would like to be able to iterate to open multiple URLs by m

相关标签:
1条回答
  • 2020-12-12 06:12

    You can escape % by doubling it:

    >>> 'http://www.example.com/BLABLA%%123BLABLApage=%d' % (1,)
    'http://www.example.com/BLABLA%123BLABLApage=1'
    

    Alternatively, use str.format() formatting instead:

    >>> 'http://www.example.com/BLABLA%123BLABLApage={:d}'.format(1)
    'http://www.example.com/BLABLA%123BLABLApage=1'
    
    0 讨论(0)
提交回复
热议问题