Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Re: True/False formats as 1/0 in a fixed width string

Reply
Thread Tools

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

 
 
Dave Angel
Guest
Posts: n/a
 
      03-27-2013
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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: True/False formats as 1/0 in a fixed width string Frank Millman Python 0 03-27-2013 09:22 AM
Re: True/False formats as 1/0 in a fixed width string Frank Millman Python 0 03-27-2013 09:14 AM
Re: True/False formats as 1/0 in a fixed width string Peter Otten Python 0 03-27-2013 08:52 AM
True/False formats as 1/0 in a fixed width string Frank Millman Python 0 03-27-2013 08:40 AM
Free Fixed-Width/Fixed-Pitch fonts? johnp HTML 4 05-23-2005 06:14 AM



Advertisments