[Tutor] blank space after a number
Enih Gilead
egilead at gmail.com
Tue Dec 28 22:10:01 CET 2010
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<egilead at gmail.com> 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
More information about the Tutor
mailing list