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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T20:22:33+00:00 2026-05-25T20:22:33+00:00

I have the following Python 2.x code, which generates a header row for tabular

  • 0

I have the following Python 2.x code, which generates a header row for tabular data:

headers = ['Name', 'Date', 'Age']
maxColumnWidth = 20 # this is just a placeholder
headerRow = "|".join( ["%s" % k.center(maxColumnWidth) for k in headers] )
print(headerRow)

This code outputs the following:

         Name          |        Date        |          Age         

Which is exactly what I want – the data is nicely formatted and centered in columns of width maxColumnWidth. (maxColumnWidth is calculated earlier in the program)

According to the Python docs, you should be able to do the same thing in Python3 with curly brace string formatting, as follows:

headerRow = "|".join( ["{:^maxColumnWidth}".format(k) for k in headers] )

However, when I do this, I get the following:

ValueError: Invalid conversion specification

But, if I do this:

headerRow = "|".join( ["{:^30}".format(k) for k in headers] )

Everything works fine.
My question is: How do I use a variable in the format string instead of an integer?:

headerRow = "|".join( ["{:^maxColumnWidth}".format(k) for k in headers] )
  • 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-25T20:22:34+00:00Added an answer on May 25, 2026 at 8:22 pm
    headers = ['Name', 'Date', 'Age']
    maxColumnWidth=21
    headerRow = "|".join( "{k:^{m}}".format(k=k,m=maxColumnWidth) for k in headers )
    print(headerRow)
    

    yields

            Name        |        Date        |        Age         
    
    1. You can represent the width maxColumnWidth as {m}, and then
      substitute the value through a format parameter.
    2. No need to use brackets (list comprehension) inside the join. A
      generator expression (without brackets) suffices.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following python code that generates a list of anonymous functions: basis
I have the following Python code: import xml.dom.minidom import xml.parsers.expat try: domTree = ml.dom.minidom.parse(myXMLFileName)
I have the following Python code: cursor.execute("INSERT INTO table VALUES var1, var2, var3,") where
I have following python code: def scrapeSite(urlToCheck): html = urllib2.urlopen(urlToCheck).read() from BeautifulSoup import BeautifulSoup
I have the following code which I use to map a nested list in
I have the following code in Python: def point_to_index(point): if point not in points:
I have a generated file with thousands of lines like the following: CODE,XXX,DATE,20101201,TIME,070400,CONDITION_CODES,LTXT,PRICE,999.0000,QUANTITY,100,TSN,1510000001 Some
I have written the following Python code: #!/usr/bin/python # -*- coding: utf-8 -*- import
I have the following code which adds a toolbar icon gtk-info (from stock) to
I am using the following code which I have found online def c_int_binary_search(seq,t): #

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.