[Tutor] String Replacement question
Kent Johnson
kent37 at tds.net
Wed May 21 12:36:30 CEST 2008
On Wed, May 21, 2008 at 5:35 AM, Faheem <faheem at atlantiscomputing.com> wrote:
> Hi all,
> How do I replace the same value multiple times without repeating the
> same variable name/value repeatedly?
> for ex.
>
> some = 'thing'
> print '%s %s %s %s' % (some,some,some,some)
You can use named parameters, which moves the repetition to the format string:
In [24]: print '%(some)s %(some)s %(some)s %(some)s' % (dict(some=some))
thing thing thing thing
With multiple values, a common trick is to pass vars() or locals() as
the dict, giving the format access to all defined variables:
In [27]: a=1
In [28]: b=2
In [29]: print '%(a)s %(b)s' % vars()
1 2
If you literally need to repeat as in your example, you could do this:
In [26]: print '%s %s %s %s' % (4*(some,))
thing thing thing thing
Kent
More information about the Tutor
mailing list