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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T09:04:32+00:00 2026-05-27T09:04:32+00:00

I am a bit new to Python, having dabbled in VB.NET, C, and a

  • 0

I am a bit new to Python, having dabbled in VB.NET, C, and a few other odd languages, so I am finding the approaches used before don’t always apply to Python. In this case, I want to print out a series of integer values into columns and center them in as small amount of code as possible.

With plain strings, this is easy as:

print "%s|%s|%s|%s"   \
      % (s1.center(w), s2.center(x), s3.center(y), s4.center(z))

Where w through z is some integer value specifying the width of the field that the strings, s1 through s4, should be center in.

Doing the same with numbers when I want to use format specifiers like %0.2x or %4d won’t work because integers don’t have a center function.

Using a more C-oriented approach, I could convert each integer independently:

s1 = str("%0.4x" % (i)).center(w)
s2 = str("%0.2x" % (i)).center(x)
s3 = str("%0.2x" % (i)).center(y)
s4 = str("%0.8x" % (i)).center(z)
print "%s|%s|%s|%s" % (s1, s2, s3, s4)

But that seems to be “unpythonic”, if I am picking up the lingo correctly. What would be a good “pythonic” way to do this? It needs to be something that works with Python 2.4 and 2.7 (I am working in both environments).

Thanks!

  • 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-27T09:04:33+00:00Added an answer on May 27, 2026 at 9:04 am
    ints = [1, 2, 3, 4]
    widths = [(4, w), (2, x), (2, y), (8, z)]
    for i in ints:
        print "%s|%s|%s|%s" % tuple(((("%0." + str(padding) + "d") % (i)).center(width)) for padding, width in widths)
    

    Each tuple in widths is (padding, width), where each padding associated with a width is used in the string formatting "%0.[padding]d".

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

Sidebar

Related Questions

I am completely new to Python-- never used it before today. I am interested
First of all I am new to python and a bit rusty on .NET
I'm new to python and experimenting a bit but having trouble making a list
I'm a bit new to WCF and I don't think I completely understand what
I'm a bit new to .Net development, been working in Java for some time
I'm having a great time learning Python, but I've just gotten a bit stuck
I am new to python and trying to create a bloomFilter based on Bit
I'm fairly new to Python and programming in general. I have done a few
I'm new to Python, and a bit rusty with my linear algebra, so perhaps
I'm relatively new to python, have done it a bit for my physics university

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.