Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3285502
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T20:16:27+00:00 2026-05-17T20:16:27+00:00

I was going through http://web2py.com/book/default/chapter/02 and found this: >>> print ‘number is ‘ +

  • 0

I was going through http://web2py.com/book/default/chapter/02 and found this:

>>> print 'number is ' + str(3)
number is 3
>>> print 'number is %s' % (3)
number is 3
>>> print 'number is %(number)s' % dict(number=3)
number is 3

It has been given that The last notation is more explicit and less error prone, and is to be preferred.

I am wondering what is the advantage of using the last notation.. will it not have a performance overhead?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-17T20:16:28+00:00Added an answer on May 17, 2026 at 8:16 pm
    >>> print 'number is ' + str(3)
    number is 3
    

    This is definitely the worst solution and might cause you problems if you do the beginner mistake "Value of obj: " + obj where obj is not a string or unicode object. For many concatenations, it’s not readable at all – it’s similar to something like echo "<p>Hello ".$username."!</p>"; in PHP (this can get arbitrarily ugly).


    print ‘number is %s’ % (3)
    number is 3

    Now that is much better. Instead of a hard-to-read concatenation, you see the output format immediately. Coming back to the beginner mistake of outputting values, you can do print "Value of obj: %r" % obj, for example. I personally prefer this in most cases. But note that you cannot use it in gettext-translated strings if you have multiple format specifiers because the order might change in other languages.

    As you forgot to mention it here, you can also use the new string formatting method which is similar:

    >>> "number is {0}".format(3)
    'number is 3'
    

    Next, dict lookup:

    >>> print 'number is %(number)s' % dict(number=3)
    number is 3
    

    As said before, gettext-translated strings might change the order of positional format specifiers, so this option is the best when working with translations. The performance drop should be negligible – if your program is not all about formatting strings.

    As with the positional formatting, you can also do it in the new style:

    >>> "number is {number}".format(number=3)
    'number is 3'
    

    It’s hard to tell which one to take. I recommend you to use positional arguments with the % notation for simple strings and dict lookup formatting for translated strings.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I been going through this tutorial http://www.codeproject.com/KB/linq/BulkOperations_LinqToSQL.aspx and them make a SP like this
So I am following this guide: http://technotes.1000lines.net/?p=23 and I am going through the steps.
I was going through C# Brainteasers ( http://www.yoda.arachsys.com/csharp/teasers.html ) and came across one question:
I have been reviewing model binding with collections, specifically going through this article http://weblogs.asp.net/nmarun/archive/2010/03/13/asp-net-mvc-2-model-binding-for-a-collection.aspx
im going through the setup on http://www.railstutorial.org/chapters/static-pages#fig:autotest_green and im stuck on this particular error:
I'm going through the Sharded Counters example in Java: http://code.google.com/appengine/articles/sharding_counters.html I have a question
After going through the Appendix A, C# Coding Style Conventions of the great book
I was going through the AWDR book on web development with ruby on rails
I'm going through the full tutorial at cplusplus.com, coding and compiling each example manually.
I have been struggling with this all day and have tried going through the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.