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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T19:03:41+00:00 2026-05-30T19:03:41+00:00

I read that I can use Python’s Format Specification Mini-Language to have more control

  • 0

I read that I can use Python’s Format Specification Mini-Language to have more control over how strings are displayed. However, I am having a hard time figuring out how to use it to display floats aligned on the decimal point.

For example, say I have the following three lists:

job_IDs = ['13453', '123', '563456'];
memory_used = [30, 150.54, 20.6];
memory_units = ['MB', 'GB', 'MB'];

I would like to iterate through these three lists and print

Job 13453:   30      MB
Job 123:    150.54   MB
Job 563456:  20.6    GB

So far I have tried:

for i in range(len(jobIDs)):
    my_str = "{item:15}{value:6} {units:3}".format( 
    item='Job ' + job_IDs[i] + ':' , value=str(memories[i]),units=memory_units[i]);

    print my_str

which prints:

Job 13453:   30      MB
Job 123:     150.54  MB
Job 563456:  20.6    GB

which is almost right, but it does not align the floats around the decimal point. How can I use Python’s Format Specification Mini-Language to do it the way I need?

  • 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-30T19:03:43+00:00Added an answer on May 30, 2026 at 7:03 pm

    This is what you want:

    for i in range(len(job_IDs)):
        print "Job {item:15} {value[0]:>6}.{value[1]:<6} {units:3}".format(item=job_IDs[i]+':', value=memory_used[i].split('.') if '.' in memory_used[i] else (memory_used[i], '0'), units=memory_units[i])
    

    Here is how it works:

    This is the main part: value=memory_used[i].split('.') if '.' in memory_used[i] else (memory_used[i], '0'), which means: if there is a decimal point, split the string as the whole and decimal part, or set the decimal part to 0.

    Then in the format string: {value[0]:>6}.{value[1]:<6} means, the whole part shifted right, followed by a dot, then the decimal part shifted left.

    which prints:

    Job 13453:              30.0      MB
    Job 123:               150.54     GB
    Job 563456:             20.6      MB
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have read in a several places (e.g. here ) that I can use
Can anyone recommend a simple API that will allow me to use read a
I want to read the html file.And for that I use System.IO.File.ReadAllText(path) .It can
I have read that you can do it, but would this really improve performance
I am developing in python a file class that can read and write a
I like the new Dynamic keyword and read that it can be used as
Is this warning anything to worry about? I've read that it can cause erratic
I'm building a plugin system for my application. I've read that anyone can decomple
Is there any tool that can read a WSDL file and based on the
Is there any class in the .NET framework that can read/write standard .ini files:

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.