problems using % in strings with %(var)s replacements

Michael Haggerty mhagger at alum.mit.edu
Thu May 24 16:19:53 CEST 2001


"Thomas Weholt" <thomas at cintra.no> writes:

> Say I got a string s = "%(number)s among 50% are efficient for
> %(customer_name)s."
> [...]
> I see the % in 50% is the problem but how can I use the %-char in strings
> and still use %(...)s replacements??

Use '%%' to get a single '%':

    >>> s = "%(number)s among 50%% are efficient for %(customer_name)s."
    >>> s % {'number':42,'customer_name':'Acme Inc.'}
    '42 among 50% are efficient for Acme Inc..'

Michael

-- 
Michael Haggerty
mhagger at alum.mit.edu



More information about the Python-list mailing list