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


Groups > comp.lang.python > #41996

Re: True/False formats as 1/0 in a fixed width string

Path csiph.com!usenet.pasdenom.info!weretis.net!feeder1.news.weretis.net!feeder.erje.net!eu.feeder.erje.net!newsfeed.freenet.ag!news2.euro.net!newsgate.cistron.nl!newsgate.news.xs4all.nl!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.001
X-Spam-Evidence '*H*': 1.00; '*S*': 0.00; 'value,': 0.04; 'cpython': 0.05; 'modify': 0.07; 'purpose.': 0.07; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'subject:string': 0.09; 'width': 0.09; 'boolean': 0.16; 'dave.': 0.16; 'length.': 0.16; 'means.': 0.16; 'received:80.91.229.3': 0.16; 'received:plane.gmane.org': 0.16; 'repr()': 0.16; 'somewhere.': 0.16; 'str()': 0.16; 'supported:': 0.16; '(you': 0.16; 'thanks,': 0.17; 'wrote:': 0.18; 'variable': 0.18; 'bit': 0.19; 'solution.': 0.20; 'work,': 0.20; '>>>': 0.22; 'manual': 0.22; 'header:User-Agent:1': 0.23; 'instead.': 0.24; 'specify': 0.24; 'string,': 0.24; 'subject:/': 0.26; 'header:X -Complaints-To:1': 0.27; 'header:In-Reply-To:1': 0.27; 'fixed': 0.29; 'skip:p 30': 0.29; 'am,': 0.29; 'database,': 0.30; "skip:' 10": 0.31; 'flags': 0.31; 'obscure': 0.31; 'really,': 0.31; 'another': 0.32; "can't": 0.35; 'something': 0.35; 'but': 0.35; 'there': 0.35; "didn't": 0.36; 'useful': 0.36; 'two': 0.37; 'easily': 0.37; 'easiest': 0.38; 'version,': 0.38; 'work?': 0.38; 'to:addr:python-list': 0.38; 'sure': 0.39; 'to:addr:python.org': 0.39; 'received:org': 0.40; 'dave': 0.60; 'conversion': 0.61; 'making': 0.63; 'worth': 0.66; 'frank': 0.68; "'true'": 0.84; 'column.': 0.84; 'surround': 0.84; 'angel': 0.91; 'subject:True': 0.91
X-Injected-Via-Gmane http://gmane.org/
To python-list@python.org
From Frank Millman <frank@chagford.com>
Subject Re: True/False formats as 1/0 in a fixed width string
Date Wed, 27 Mar 2013 11:22:16 +0200
References <kiub99$q9u$1@ger.gmane.org> <5152B407.50801@davea.name>
Mime-Version 1.0
Content-Type text/plain; charset=ISO-8859-1; format=flowed
Content-Transfer-Encoding 7bit
X-Gmane-NNTP-Posting-Host 197.87.50.233
User-Agent Mozilla/5.0 (Windows NT 5.2; rv:14.0) Gecko/20120713 Thunderbird/14.0
In-Reply-To <5152B407.50801@davea.name>
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 <http://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 <http://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.3801.1364376154.2939.python-list@python.org> (permalink)
Lines 58
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1364376154 news.xs4all.nl 6863 [2001:888:2000:d::a6]:46016
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:41996

Show key headers only | View raw


On 27/03/2013 10:55, Dave Angel wrote:
> On 03/27/2013 04:40 AM, Frank Millman wrote:
>> Hi all
>>
>> This is a bit of trivia, really, as I don't need a solution.
>>
>> But someone might need it one day, so it is worth mentioning.
>>
>>  >>> '{}'.format(True)
>> 'True'
>>  >>> '{:<10}'.format(True)
>> '1         '
>>
>> One might want to format True/False in a fixed width string, but it
>> returns 1/0 instead. Is there any way to make this work?
>>
>> Frank Millman
>>
>
> Easiest way is to surround the boolean variable with repr()
>
> flag = True
> '{:<10}'.format(repr(flag))
>
> An alternative is to just use something like:
>
> ["False     ","True      "][flag]
>
> making sure the two strings are of the same length.
>
> (You didn't specify version, but I tested these with CPython 2.7.3)
>

Thanks, Dave. I am using CPython 3.3.0, which behaves the same for this 
purpose.

Your solutions work, but in my case I am reading in data from a 
database, so I want to create a format string, and then just call

     print(format_string.format(*row))

so I can't easily modify the contents of a single column.

Peter's solution works perfectly for this scenario -

 >>> '{!s:<10}'.format(True)
'True      '

I had to look up what '!s' means. From the manual -

"Three conversion flags are currently supported: '!s' which calls str() 
on the value, '!r' which calls repr() and '!a' which calls ascii()."

Another obscure but useful tip to store away somewhere.

Frank

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


Thread

Re: True/False formats as 1/0 in a fixed width string Frank Millman <frank@chagford.com> - 2013-03-27 11:22 +0200

csiph-web