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

  • Home
  • SEARCH
  • 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 8213415
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T11:06:33+00:00 2026-06-07T11:06:33+00:00

I want to pad a string to a certain length, depending on the value

  • 0

I want to pad a string to a certain length, depending on the value of a variable, and I’m wondering if there is a standard, Pythonic way to do this using the string.format mini-language. Right now, I can use string concatenation:

padded_length = 5
print(("\n{:-<" + str((padded_length)) + "}").format("abc"))
# Outputs "abc--"

padded_length = 10
print(("\n{:-<" + str((padded_length)) + "}").format("abc"))
#Outputs "abc-------"

I tried this method:

print(("{:-<{{padded_length}}}".format(padded_length = 10)).format("abc"))

but it raises an IndexError: tuple index out of range exception:

Traceback (most recent call last):
  File "<pyshell#41>", line 1, in <module>
    print(("{:-<{{padded_length}}}".format(padded_length = 10)).format("abc"))
IndexError: tuple index out of range

Is there a standard, in-built way to do this apart from string concatenation? The second method should work, so I’m not sure why it fails.

  • 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-07T11:06:34+00:00Added an answer on June 7, 2026 at 11:06 am
    print(("\n{:-<{}}").format("abc", padded_length))
    

    The other way you were trying, should be written this way

    print(("{{:-<{padded_length}}}".format(padded_length=10)).format("abc"))
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

want to know why String behaves like value type while using ==. String s1
I want to pad some percentage values so that there are always 3 units
I want build a sketch pad app on iPhone, I assume that this type
I want this to work on all screen sizes so I don't think using
How can I pad a string with spaces on the left when using printf?
i want to find a efficent way to do : i have a string
Can I use String.Format() to pad a certain string with arbitrary characters? Console.WriteLine(->{0,18}<-, hello);
I want to encrypt a string using AES with my own key. But I'm
Is there a way to pad or decompress an image (JPEG) in bash? By
Want to code a key pad for an calculator. What I want to make

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.