Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #73541 > unrolled thread
| Started by | MRAB <python@mrabarnett.plus.com> |
|---|---|
| First post | 2014-06-24 12:09 +0100 |
| Last post | 2014-06-24 12:09 +0100 |
| Articles | 1 — 1 participant |
Back to article view | Back to comp.lang.python
This discussion starts older than the indexed window; earlier articles aren't shown. The article labeled Started by
below is the oldest one visible, not the original post.
Re: Format String: Only when value supplied MRAB <python@mrabarnett.plus.com> - 2014-06-24 12:09 +0100
| From | MRAB <python@mrabarnett.plus.com> |
|---|---|
| Date | 2014-06-24 12:09 +0100 |
| Subject | Re: Format String: Only when value supplied |
| Message-ID | <mailman.11214.1403608151.18130.python-list@python.org> |
On 2014-06-24 10:18, Florian Lindner wrote:
> Hello,
>
> I have a format string like:
>
> print "{:10} {:25} = {:6} ({})".format(mod, name, value, description)
>
> description can be None. In this case I want to print an empty string (which
> can be achieved by replacing it with 'description or ""') and I want to omit
> the brackets. Is there a way to tell the format string to omit a part if an
> input variable is None?
>
You could strip the trailing brackets (and spaces too?) after
formatting:
print "{:10} {:25} = {:6} ({})".format(mod, name, value,
description or "").rstrip("() ")
assuming, of course, that the value never ends with brackets.
> Another question: value can be bool. When I format value with just {} if
> prints True or False, when I use {:6} it prints 1 or 0. Is there a way to
> get pack to True / False?
>
Back to top | Article view | comp.lang.python
csiph-web