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


Groups > comp.lang.python > #41993

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

Path csiph.com!usenet.pasdenom.info!aioe.org!news.stack.nl!newsfeed.xs4all.nl!newsfeed1.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail
Return-Path <davea@davea.name>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.029
X-Spam-Evidence '*H*': 0.94; '*S*': 0.00; 'cpython': 0.05; 'subject:string': 0.09; 'width': 0.09; 'boolean': 0.16; 'length.': 0.16; 'received:74.208.4.195': 0.16; 'repr()': 0.16; '(you': 0.16; 'wrote:': 0.18; 'variable': 0.18; 'bit': 0.19; 'solution.': 0.20; '>>>': 0.22; 'header:User-Agent:1': 0.23; 'instead.': 0.24; 'specify': 0.24; 'string,': 0.24; 'subject:/': 0.26; 'header:In- Reply-To:1': 0.27; 'fixed': 0.29; 'am,': 0.29; "skip:' 10": 0.31; 'really,': 0.31; 'something': 0.35; 'but': 0.35; 'there': 0.35; "didn't": 0.36; 'two': 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; 'making': 0.63; 'worth': 0.66; 'frank': 0.68; 'received:74.208': 0.68; "'true'": 0.84; 'surround': 0.84; 'subject:True': 0.91
Date Wed, 27 Mar 2013 04:55:35 -0400
From Dave Angel <davea@davea.name>
User-Agent Mozilla/5.0 (X11; Linux x86_64; rv:17.0) Gecko/20130308 Thunderbird/17.0.4
MIME-Version 1.0
To python-list@python.org
Subject Re: True/False formats as 1/0 in a fixed width string
References <kiub99$q9u$1@ger.gmane.org>
In-Reply-To <kiub99$q9u$1@ger.gmane.org>
Content-Type text/plain; charset=ISO-8859-1; format=flowed
Content-Transfer-Encoding 7bit
X-Provags-ID V02:K0:Fa3YGT4KGQ7MRnnljjNi16CRpEXV9cVcMwXSq5BxJkp 8+NNPE0JkS9LEKcXwpWL+0okur6BXWfos+lPJH+NV5/A5g9N/X 16TNBQtE4OCEOFH5HC+YmU49oE5IWpb1/JIHkLpvWk+yAJU6wC lnQi+SH/ic38jcXQQyN/8VFKsNQEeVOmmdPkFNX1i2TzSJSgD8 773OF8KKixOy7BuLaBL0lXq7uGcwK1NlTfAB4+0LzgYLSOJ6FI 2IPMT4mi0Jyfl8oXi5RMrilTaCaUjINBx6kot9pAuQqbAQDLOx 30iROJSnFmF1Ws9V0dihoMhRi87z0IEn048A0yjNP/gGMw0pQ= =
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.3799.1364374559.2939.python-list@python.org> (permalink)
Lines 33
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1364374559 news.xs4all.nl 6940 [2001:888:2000:d::a6]:60822
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:41993

Show key headers only | View raw


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)

-- 
DaveA

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


Thread

Re: True/False formats as 1/0 in a fixed width string Dave Angel <davea@davea.name> - 2013-03-27 04:55 -0400

csiph-web