Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #73538

Re: Format String: Only when value supplied

Path csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!eu.feeder.erje.net!newsfeed.datemas.de!rt.uk.eu.org!newsfeed.xs4all.nl!newsfeed4.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail
Return-Path <python-python-list@m.gmane.org>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.000
X-Spam-Evidence '*H*': 1.00; '*S*': 0.00; 'else:': 0.03; 'value,': 0.04; 'string': 0.09; 'false,': 0.09; 'omit': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'brackets.': 0.16; 'none.': 0.16; 'received:80.91.229.3': 0.16; 'received:plane.gmane.org': 0.16; 'subject:String': 0.16; 'subject:when': 0.16; 'templating': 0.16; 'wrote:': 0.18; 'variable': 0.18; 'pieces': 0.19; 'replacing': 0.19; 'input': 0.22; 'print': 0.22; 'header:User-Agent:1': 0.23; 'lets': 0.24; 'received:comcast.net': 0.24; 'this:': 0.26; 'header:X-Complaints- To:1': 0.27; 'header:In-Reply-To:1': 0.27; 'am,': 0.29; 'skip:( 20': 0.30; '(which': 0.31; 'prints': 0.31; 'question:': 0.31; 'option': 0.32; 'another': 0.32; 'there': 0.35; 'to:addr:python- list': 0.38; 'to:addr:python.org': 0.39; 'received:org': 0.40; 'tell': 0.60; 'real': 0.63; 'skip:n 10': 0.64; 'florian': 0.84; 'subject:Only': 0.84
X-Injected-Via-Gmane http://gmane.org/
To python-list@python.org
From Ned Batchelder <ned@nedbatchelder.com>
Subject Re: Format String: Only when value supplied
Date Tue, 24 Jun 2014 06:28:11 -0400
References <lobfpf$6ta$1@ger.gmane.org>
Mime-Version 1.0
Content-Type text/plain; charset=ISO-8859-1; format=flowed
Content-Transfer-Encoding 7bit
X-Gmane-NNTP-Posting-Host c-50-133-228-126.hsd1.ma.comcast.net
User-Agent Mozilla/5.0 (Macintosh; Intel Mac OS X 10.8; rv:24.0) Gecko/20100101 Thunderbird/24.6.0
In-Reply-To <lobfpf$6ta$1@ger.gmane.org>
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.15
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <https://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list/>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <https://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.11213.1403605711.18130.python-list@python.org> (permalink)
Lines 34
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1403605711 news.xs4all.nl 2878 [2001:888:2000:d::a6]:54331
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:73538

Show key headers only | View raw


On 6/24/14 5:18 AM, 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?

.format() is not that sophisticated.  One option is to build your string 
in pieces like this:

     if description:
         nice_description = "   ({})".format(description)
     else:
         nice_description = ""
     print "{:10} {:25} = {:6}{}".format(mod, name, value, nice_description)

Another option is to use a real templating engine like Mako or Jinja 
that lets you use conditionals.

>
> 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?

You can force it to be a string  with "{!s:6}"

-- 
Ned Batchelder, http://nedbatchelder.com

Back to comp.lang.python | Previous | Next | Find similar | Unroll thread


Thread

Re: Format String: Only when value supplied Ned Batchelder <ned@nedbatchelder.com> - 2014-06-24 06:28 -0400

csiph-web