On 5/5/08, phillc ([EMAIL PROTECTED]) wrote:

>fourth:
>====
>i never understood this, why do people do
>
>somevar = "blah blah %s" % anothervar
>
>instead of "blah bla" + anothervar
>?

Because you can perform specific string formatting operations at the same time. 
Plus, the formatting and data are separated, not to mention the readability IMO 
is better as you'll see below.

Take for example a float value of 3.141526. Depending upon the circumstance, 
you may wish to round this value but you don't want to lose the precision.

    >>> '%0.2f' % 3.1425
    '3.14'

It gets _much_ better as there is the ability to name that value for 
formatting. There are a couple of ways to tap into this but the one I use all 
the time is from a dictionary, here is a simplistic example.

First, you need a dictionary, here's a simple one:

    >>> somedict = {}
    >>> somedict['first_name'] = 'Wilber'
    >>> somedict['last_name'] = 'Snodtgrass'
    >>> somedict['salutation'] = 'Mr.'

Then, when ready, you can use string formatting like this:

    >>> 'Dear %(salutation)s %(first_name)s %(last_name)s,' % somedict
    'Dear Mr. Wilber Snodtgrass,'

Sure beats the snod out of:

    >>> 'Dear ' + somedict['salutation'] + \
    ...     ' ' + somedict['first_name'] + ' ' + \
    ...     somedict['last_name'] + ','
    'Dear Mr. Wilber Snodtgrass,'

As you can see, complex string formatting is possible. My examples only show a 
the shiny tail-pipe of a very sweet and powerful vehicle.

HTH

Scott

PS. If all you are doing is terminating a line, then this is fine: 
        text_block + '/n'

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to