[Tutor] blank space after a number

bob gailer bgailer at gmail.com
Tue Dec 28 22:32:36 CET 2010


On 12/28/2010 2:36 PM, Alan Gauld wrote:
>
> "Enih Gilead" <egilead at gmail.com> wrote
>
>> Just as an example, the 'a' that is made = to '0', when printed, it 
>> comes with a blank space after...
>>
>> a, b = 0, 1
>> while b < 10:
>>     print a, b,
>>     a, b = a, b + 1
>>
>> ... do you think is there any reasonable way to transform the '0' 
>> number into string and then back to numeral - after the printing action?
>
> You can use string formatting to create the string any way tou want:
>
> print "%d%d" % a,b

Erm... print "%d%d" % (a,b)
>
> will print 01
>
> Or you can use string conversion and catenastion:
>
> print str(a)+str(b)
>
> to get a similar result.
>
> Incidentally your loop would be clearer if you omited the tuple 
> assignment:
>
> while b < 10:
>     print a, b,
>     b +=  1
>
> And if you only use 'a' to zero pad the number you can do that with
> string formatting too:
>
> print "%02d" % b
>
>
> Personally I'd go for string formatting because of its much more
> powerful and flexible options.
>
>


-- 
Bob Gailer
919-636-4239
Chapel Hill NC



More information about the Tutor mailing list