Re: [Tutor] blank space after a number

2010-12-28 Thread bob gailer

On 12/28/2010 8:09 PM, bob gailer wrote:

Now that I understand what you want -

for b in range(120):
 print '%0*i' % (max(2,int(math.log10(b))), b)


Sorry forgot to add
import math

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

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] blank space after a number

2010-12-28 Thread Alan Gauld

"Enih Gilead"  wrote


a, b = 0, 1
while b < 10:
  print '%i%i' % (a,b) + ',',
  b = b+1


If you are using string formatting it's best to get the format
string to do as much of the work as possible. In this case
forget about 'a' and just insert the zero into the string, and
similarly don't add a comma and space, just put it in the string
And for a fixed number of iterations a for loop is usually
preferable:

for b in range(1,10):
   print "%02d, " % b


Does the same job.

'''
01, 02, 03, 04, 05, 06, 07, 08, 09, '''




while b < 100:
  print '%i' % (b) + ',',
  b = b+1



And exactly the same code with the limit increased

for b in range(1,101):   #NB 101 to get the 100 - your while loop goes 
to 99...

   print "%02d, " % b


'''
01, 02, 03, 04, 05, 06, 07, 08, 09, 10, 11, 12, 13, 14, 15, 16, 17, 
18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 
35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 
52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 
69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 
86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 100 '''


The %02d says print the number as two characters, padding with zeros
as needed.

HTH,


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


___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] blank space after a number

2010-12-28 Thread bob gailer

Now that I understand what you want -

for b in range(120):
 print '%0*i' % (max(2,int(math.log10(b))), b)

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

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] blank space after a number

2010-12-28 Thread Noah Hall
I'll just add there's a better way to do both of the examples you've done
there -


> > a, b = 0, 1
> > while b < 10:
> >print '%i%i' % (a,b) + ',',
> >b = b+1


An easier way of doing this is to instead do the following, including the a
(although not needed, as simply using 0 would work) -

a, b = 0, 1
while b <10:

print '%i%i,' % (a,b),
b += 1

> while b < 100:
> > print '%i' % (b) + ',',
> > b = b+1
>

while b < 100:

print '%i,' % (b),
b += 1

Or, using generators and a *for *loop (which is more suited to the task than
a *while *loop):

def stringify(x):

if x < 10:

return '0' + str(x)

else:

return str(x)

print ','.join(stringify(x) for x in xrange(1,101))
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] blank space after a number

2010-12-28 Thread Enih Gilead

Thanks a lot,
Hugo Yoshi!

As a beginner in Python, I do my best to satisfy my curiosity about some 
features in the language; and, this one was the most difficult among my 
"projects"!  :c)


Best regards,
Enih Gil'ead



# Formatting numbers from '01,' to '100'
# Many thanks to Bob Gailer and to Hugo Yoshi

a, b = 0, 1
while b < 10:
  print '%i%i' % (a,b) + ',',
  b = b+1
  # what results in:
'''
01, 02, 03, 04, 05, 06, 07, 08, 09, '''

while b < 100:
  print '%i' % (b) + ',',
  b = b+1
  # what results in:
'''
01, 02, 03, 04, 05, 06, 07, 08, 09, 10, 11, 12, 13, 14, 15, 16, 17, 18, 
19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 
37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 
55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 
73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 
91, 92, 93, 94, 95, 96, 97, 98, 99, 100 '''


print 100






On 12/28/2010 05:31 PM, Hugo Arts wrote:

On Tue, Dec 28, 2010 at 2:54 PM, Enih Gilead  wrote:

Hi, Abrahamsen!

Would you mind tell me a way to eliminate the blank space in front of "a"
[int number] ?
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?


If you put commas between things in a print statement, python will put
a space in between. Either use string formatting or convert to str()
and add together:

print "{0}{1}".format(a, b),
print str(a) + str(b),

Hugo


--
Shalom be'Shem 'Adonay, Enih Gil'ead
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] blank space after a number

2010-12-28 Thread bob gailer

On 12/28/2010 2:36 PM, Alan Gauld wrote:


"Enih Gilead"  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

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] blank space after a number

2010-12-28 Thread Alan Gauld


"Enih Gilead"  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/




___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] blank space after a number

2010-12-28 Thread bob gailer

On 12/28/2010 8:54 AM, Enih Gilead wrote:

Hi, Abrahamsen!

Would you mind tell me a way to eliminate the blank space in front of 
"a" [int number] ?
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?


Use formatting: print '%i%i' % (a,b)



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

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] blank space after a number

2010-12-28 Thread Hugo Arts
On Tue, Dec 28, 2010 at 2:54 PM, Enih Gilead  wrote:
> Hi, Abrahamsen!
>
> Would you mind tell me a way to eliminate the blank space in front of "a"
> [int number] ?
> 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?
>

If you put commas between things in a print statement, python will put
a space in between. Either use string formatting or convert to str()
and add together:

print "{0}{1}".format(a, b),
print str(a) + str(b),

Hugo
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


[Tutor] blank space after a number

2010-12-28 Thread Enih Gilead

Hi, Abrahamsen!

Would you mind tell me a way to eliminate the blank space in front of 
"a" [int number] ?
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?


Thanks beforehand ve'Shalom be'Shem 'Adonay,
Enih Gil'ead
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor