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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T19:33:52+00:00 2026-06-02T19:33:52+00:00

I have searched the forums for a little bit now and I am still

  • 0

I have searched the forums for a little bit now and I am still very confused. If someone could direct me to an article that explains this more in depth I would greatly appreciate it. I am taking an intro to python course and I found the solution to my problem, but I want to actually know how\why it works. The assignment is to return the batting average for a player, and display it as the MLB would display. So for example 4hits in 8 at bats would be .500. Here is the code I found that works.

from __future__ import division, print_function

def print_batting_average(at_bats, hits):
    average_str = "%.3f" % (hits / at_bats)
    if average_str.startswith("0"):
        average_str = average_str[1:]
    print("Batting average is", average_str)

print_batting_average(4, 2)

I know the % is modulus and gives the remainder of something. I just don’t understand how that fits into other parts of code. For example the “%.3f”. I have also seen the % used in different ways, and I just wanted to find a good explanation. Hopefully I don’t get flamed for positing this. Thanks for any help.

  • 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-02T19:33:53+00:00Added an answer on June 2, 2026 at 7:33 pm

    In the context you are asking about the % is used to help format output, and has nothing to do with any mod operation.

    For instance, %3d, %.2f are all different ways to format output. The first example means you want to display an integer and reserve 3 spaces for it. The 2nd one means you want to display a float type value with 2 numbers after the decimal point.

    SeeString Formatting Operations in
    http://docs.python.org/library/stdtypes.html#string-formatting-operations for more information and details about the various formatting types and options.

    You may come across something like this:

    number_of_Widgets = 5
    cost = 66.8788
    print 'Total cost for %d widgets is $ %5.2f.' % (number_of_Widgets, cost)
    

    yields:

    Total cost for 5 widgets is $ 66.88.
    

    The part enclosed in ‘ ‘ uses formatting instructions as place holders for the actual variable values supplied in toward the end of the line. The values are preceded by the % and then the () contain the variables that supply the values.

    Here is a simple example as to why the format strings can come in handy for formatting your output:

    Here we don’t reserve and additional space for the number and you can see it shifting the output when it reaches 10.

    In [9]: for i in xrange(5,15):
       ...:     print '%d is the number' % i
       ...:
    5 is the number
    6 is the number
    7 is the number
    8 is the number
    9 is the number
    10 is the number
    11 is the number
    12 is the number
    13 is the number
    14 is the number
    

    Here we format the number with two spaces, and get a leading blank space for single digit numbers.

    In [10]: for i in xrange(5,15):
       ....:     print '%2d is the number' % i
       ....:
     5 is the number
     6 is the number
     7 is the number
     8 is the number
     9 is the number
    10 is the number
    11 is the number
    12 is the number
    13 is the number
    14 is the number
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have searched various forums and links but havn't got anything helpful yet. Actually
I have searched for this, and i found that for api less than 12,
I have searched up and down the internet for a jQuery widget that does
I have searched all morning and yesterday afternoon and still cannot find an solution
I have searched apple's documentation and other posts on Stack Overflow, but I'm still
I have searched the forums and all over the web and have not really
OK so I have tried a few differrent ways and searched the forums. I've
I have searched many places and rarely use forums for posting... So I believe
I have searched for IO issue in forums and found many questions on the
I have searched the internet and I know that in .Net we can use

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.