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


Groups > comp.lang.python > #41993 > unrolled thread

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

Started byDave Angel <davea@davea.name>
First post2013-03-27 04:55 -0400
Last post2013-03-27 04:55 -0400
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.


Contents

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

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

FromDave Angel <davea@davea.name>
Date2013-03-27 04:55 -0400
SubjectRe: True/False formats as 1/0 in a fixed width string
Message-ID<mailman.3799.1364374559.2939.python-list@python.org>
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

[toc] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web