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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T22:18:04+00:00 2026-06-04T22:18:04+00:00

I want to create a formatted string with fixed size with fixed position between

  • 0

I want to create a formatted string with fixed size with fixed position between fields. An example explains better, here there are clearly 3 distinct fields and the string is a fixed size:

XXX        123   98.00
YYYYY        3    1.00
ZZ          42  123.34

How can I apply such formatting to a string in python (2.7)?

  • 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-04T22:18:06+00:00Added an answer on June 4, 2026 at 10:18 pm

    Sure, use the .format method. E.g.,

    print('{:10s} {:3d}  {:7.2f}'.format('xxx', 123, 98))
    print('{:10s} {:3d}  {:7.2f}'.format('yyyy', 3, 1.0))
    print('{:10s} {:3d}  {:7.2f}'.format('zz', 42, 123.34))
    

    will print

    xxx        123    98.00
    yyyy         3     1.00
    zz          42   123.34
    

    You can adjust the field sizes as desired. Note that .format works independently of print to format a string. I just used print to display the strings. Brief explanation:

    10s format a string with 10 spaces, left justified by default

    3d format an integer reserving 3 spaces, right justified by default

    7.2f format a float, reserving 7 spaces, 2 after the decimal point,
    right justfied by default.

    There are many additional options to position/format strings (padding, left/right justify etc), String Formatting Operations will provide more information.

    Update for f-string mode. E.g.,

    text, number, other_number = 'xxx', 123, 98
    print(f'{text:10} {number:3d}  {other_number:7.2f}')
    

    For right alignment

    print(f'{text:>10} {number:3d}  {other_number:7.2f}')
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to create a formatted document from a list of expressions. One of
In order to create a formatted file, I want to utilize fprintf . It
Here I go again with some PLSql.. I want to know, if there's any
This is a newbie question. To create a formatted C string, I use printf
i want create a counter that retrieve the number of month, day, hour, minute
I want create an array in JSF EL. How can I do that? Is
I want create a new JavaScript object based on an existing JavaScript object. Till
I want create a excel with Apache POI in java and I must insert
i want create image animation , i have 50 images with png format now
I want create module which update list of usb devices automatically (not only mass

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.