[Tutor] about print()

2011-06-05 Thread Ryan Wu
Hi all,

I am a newbie of python, and reading 'python essential reference'.

Now I want to print this  results

'a is %d' % a -> a is 42
>

with the code

a = 42
> test = "'a is %d' % a"
> print( '%20s ->' % test, test)
>

but what I get is

> 'a is %d' % a -> 'a is %d' % a
>


What is the difference of  print(test) and print ( 'a is %d' % a )?

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


Re: [Tutor] about print()

2011-06-06 Thread Ryan Wu
Oh, I see! It's a little stupid question :)
Thanks,Alan!

On 6/6/11, Alan Gauld  wrote:
> "Ryan Wu"  wrote
>
>> I am a newbie of python, and reading 'python essential reference'.
>>
>> Now I want to print this  results
>> 'a is %d' % a -> a is 42
>>
>> with the code
>>
>> a = 42
>>> test = "'a is %d' % a"
>
> test is now a literal string
>
>>> print( '%20s ->' % test, test)
>
> And this inserts the literal string into the format string then
> prints the literal string
>
> What I think you wanted to do is:
>
>>>> a = 42
>>>> test = 'a is %d'
>
>>>> print( '%20s -> %s' % (test, test % a) )
>
>> What is the difference of  print(test) and print ( 'a is %d' % a )?
>
> In test you have the a inside the string delimiters so it is part
> of the string. In the second case a is outside the string and
> gets evaluated as a variable
>
> 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
>


-- 

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