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 6900875
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T07:36:08+00:00 2026-05-27T07:36:08+00:00

These are two very popular ways of formatting a string in Python. One is

  • 0

These are two very popular ways of formatting a string in Python. One is using a dict:

>>> 'I will be %(years)i on %(month)s %(day)i' % {'years': 21, 'month': 'January', 'day': 23}
'I will be 21 on January 23'

And the other one using a simple tuple:

>>> 'I will be %i on %s %i' % (21, 'January', 23)
'I will be 21 on January 23'

The first one is way more readable, but the second one is faster to write. I actually use them indistinctly.

What are the pros and cons of each one? regarding performance, readability, code optimization (is one of them transformed to the other?) and anything else you would think is useful to share.

  • 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-27T07:36:08+00:00Added an answer on May 27, 2026 at 7:36 am

    Why format() is more flexible than % string operations

    I think you should really stick to format() method of str, because it is the preferred way to format strings and will probably replace string formatting operation in the future.

    Furthermore, it has some really good features, that can also combine position-based formatting with keyword-based one:

    >>> string = 'I will be {} years and {} months on {month} {day}'
    >>> some_date = {'month': 'January', 'day': '1st'}
    >>> diff = [3, 11] # years, months
    >>> string.format(*diff, **some_date)
    'I will be 3 years and 11 months on January 1st'
    

    even the following will work:

    >>> string = 'On {month} {day} it will be {1} months, {0} years'
    >>> string.format(*diff, **some_date)
    'On January 1st it will be 11 months, 3 years'
    

    There is also one other reason in favor of format(). Because it is a method, it can be passed as a callback like in the following example:

    >>> data = [(1, 2), ('a', 'b'), (5, 'ABC')]
    >>> formatter = 'First is "{0[0]}", then comes "{0[1]}"'.format
    >>> for item in map(formatter, data):
        print item
    
    
    First is "1", then comes "2"
    First is "a", then comes "b"
    First is "5", then comes "ABC"
    

    Isn’t it a lot more flexible than string formatting operation?

    See more examples on documentation page for comparison between % operations and .format() method.

    Comparing tuple-based % string formatting with dictionary-based

    Generally there are three ways of invoking % string operations (yes, three, not two) like that:

    base_string % values
    

    and they differ by the type of values (which is a consequence of what is the content of base_string):

    • it can be a tuple, then they are replaced one by one, in the order they are appearing in tuple,

      >>> 'Three first values are: %f, %f and %f' % (3.14, 2.71, 1)
      'Three first values are: 3.140000, 2.710000 and 1.000000'
      
    • it can be a dict (dictionary), then they are replaced based on the keywords,

      >>> 'My name is %(name)s, I am %(age)s years old' % {'name':'John','age':98}
      'My name is John, I am 98 years old'
      
    • it can be a single value, if the base_string contains single place where the value should be inserted:

      >>> 'This is a string: %s' % 'abc'
      'This is a string: abc'
      

    There are obvious differences between them and these ways cannot be combined (in contrary to format() method which is able to combine some features, as mentioned above).

    But there is something that is specific only to dictionary-based string formatting operation and is rather unavailable in remaining three formatting operations’ types. This is ability to replace specificators with actual variable names in a simple manner:

    >>> name = 'John'
    >>> surname = 'Smith'
    >>> age = 87
    # some code goes here
    >>> 'My name is %(surname)s, %(name)s %(surname)s. I am %(age)i.' % locals()
    'My name is Smith, John Smith. I am 87.'
    

    Just for the record: of course the above could be easily replaced by using format() by unpacking the dictionary like that:

    >>> 'My name is {surname}, {name} {surname}. I am {age}.'.format(**locals())
    'My name is Smith, John Smith. I am 87.'
    

    Does anyone else have an idea what could be a feature specific to one type of string formatting operation, but not to the other? It could be quite interesting to hear about it.

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

Sidebar

Related Questions

These two methods appear to behave the same to me public IEnumerable<string> GetNothing() {
My question is very similar to these two: C# component events? C# - writing
I have to combine these two mySQL queries into one. I duplicated a solution
I have two controls. The XAML's are big and very similar. One difference is
Possible Duplicate: What is the difference between these two ways of allocating memory in
Working in c# i've found very useful two static methods of the String class
I have a very simple item ownership table with these two columns: UserID, ItemID
Let's say I have these two very basic entities: public class ParentEntity { public
These two terms are very diffused. How exactly do they differ? Is there any
These two methods exhibit repetition: public static Expression<Func<Foo, FooEditDto>> EditDtoSelector() { return f =>

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.