Inserting the same value multiple times when formatting a string Inserting the same value multiple times when formatting a string python python

Inserting the same value multiple times when formatting a string


You can use advanced string formatting, available in Python 2.6 and Python 3.x:

incoming = 'arbit'result = '{0} hello world {0} hello world {0}'.format(incoming)


incoming = 'arbit'result = '%(s)s hello world %(s)s hello world %(s)s' % {'s': incoming}

You may like to have a read of this to get an understanding: String Formatting Operations.


You can use the dictionary type of formatting:

s='arbit'string='%(key)s hello world %(key)s hello world %(key)s' % {'key': s,}