string % dictionary question
Sam Sungshik Kong
ssk at chol.nospam.net
Tue Sep 14 00:20:08 EDT 2004
Thanks for the reply.
What if the string is more complicated?
For example,
di = {}
di["name"] = "Sam"
di["age"] = None
s = "name: %(name)s, age: %(age)d" % di
Thanks.
Sam
"Jorge Godoy" <godoy at ieee.org> wrote in message
news:m3zn3tr0hy.fsf at g2ctech.com...
> "Sam Sungshik Kong" <ssk at chol.nospam.net> writes:
>
> > Hello, group!
> >
> > <code>
> >>>> di={}
> >>>> di["test"]=None
> >>>> s="%(test)s" % di
> >>>> s
> > 'None'
> > </code>
> >
> > I want the result to be just empty string when the dictionary value is
None.
> > Is there a good way?
>
> You can check it after the definition and make the necessary changes...
>
> >>> di={}
> >>> di["test"]=None
> >>> s="%(test)s" % di
> >>> if s == "None": s = ""
> ...
> >>> s
> ''
> >>> di["test"] = 1
> >>> s = "%(test)s" % di
> >>> if s == "None": s = ""
> ...
> >>> s
> '1'
> >>>
>
>
> Be seeing you,
> --
> Godoy. <godoy at ieee.org>
More information about the Python-list
mailing list