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

  • Home
  • SEARCH
  • 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 9008047
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T01:51:03+00:00 2026-06-16T01:51:03+00:00

I do this in Python 2: (%d goals, $%d) % (self.goals, self.penalties) What is

  • 0

I do this in Python 2:

"(%d goals, $%d)" % (self.goals, self.penalties)

What is the Python 3 version of this?

I tried searching for examples online but I kept getting Python 2 versions.

  • 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-06-16T01:51:04+00:00Added an answer on June 16, 2026 at 1:51 am

    Here are the docs about the “new” format syntax. An example would be:

    "({:d} goals, ${:d})".format(self.goals, self.penalties)
    

    If both goals and penalties are integers (i.e. their default format is ok), it could be shortened to:

    "({} goals, ${})".format(self.goals, self.penalties)
    

    And since the parameters are fields of self, there’s also a way of doing it using a single argument twice (as @Burhan Khalid noted in the comments):

    "({0.goals} goals, ${0.penalties})".format(self)
    

    Explaining:

    • {} means just the next positional argument, with default format;
    • {0} means the argument with index 0, with default format;
    • {:d} is the next positional argument, with decimal integer format;
    • {0:d} is the argument with index 0, with decimal integer format.

    There are many others things you can do when selecting an argument (using named arguments instead of positional ones, accessing fields, etc) and many format options as well (padding the number, using thousands separators, showing sign or not, etc). Some other examples:

    "({goals} goals, ${penalties})".format(goals=2, penalties=4)
    "({goals} goals, ${penalties})".format(**self.__dict__)
    
    "first goal: {0.goal_list[0]}".format(self)
    "second goal: {.goal_list[1]}".format(self)
    
    "conversion rate: {:.2f}".format(self.goals / self.shots) # '0.20'
    "conversion rate: {:.2%}".format(self.goals / self.shots) # '20.45%'
    "conversion rate: {:.0%}".format(self.goals / self.shots) # '20%'
    
    "self: {!s}".format(self) # 'Player: Bob'
    "self: {!r}".format(self) # '<__main__.Player instance at 0x00BF7260>'
    
    "games: {:>3}".format(player1.games)  # 'games: 123'
    "games: {:>3}".format(player2.games)  # 'games:   4'
    "games: {:0>3}".format(player2.games) # 'games: 004'
    

    Note: As others pointed out, the new format does not supersede the former, both are available both in Python 3 and the newer versions of Python 2 as well. Some may say it’s a matter of preference, but IMHO the newer is much more expressive than the older, and should be used whenever writing new code (unless it’s targeting older environments, of course).

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

Sidebar

Related Questions

I want to install this python package: http://pypi.python.org/pypi/netifaces/0.5 But I don't know how and
/Users/smcho/Desktop/bracket/[10,20] directory has abc.txt, but when I run this Python code import glob import
This Python: data = {} def f(): pass import sys help(sys.modules[__name__]) Prints this: Help
Consider this python program: import sys lc = 0 for line in open(sys.argv[1]): lc
Consider this Python code for printing a list of comma separated values for element
Consider this Python snippet: for a in range(10): if a == 7: pass if
I was given this Python code that would calculate an MD5 value for any
I'm looking for ways to express this Python snippet in Perl: data = {A:
I inadvertently marked all the messages in my inbox as read with this python
Hey brilliant minds out there, I'm trying to get this Python script that I

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.