[Tutor] blank space after a number

Alan Gauld alan.gauld at btinternet.com
Tue Dec 28 20:36:48 CET 2010


"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

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.


-- 
Alan Gauld
Author of the Learn to Program web site
http://www.alan-g.me.uk/






More information about the Tutor mailing list