woz523
woz523

Reputation: 117

how to add padding after text and variable in python string format

I would like to add space after I write text and my variable in Python Format String. So I want to align and add 50 spaces such as the below picture.

Here is what I want to do: enter image description here

I wrote this code but it does not work

print("{:<50s}{}".format("Enter the point ID of unknown point ",i,)," :",end=" ")

Upvotes: 0

Views: 625

Answers (2)

Daniel
Daniel

Reputation: 42768

You have to first format your prompt, and in a second step print this prompt:

prompt = f"Enter the point ID of unknown point {i}"
print(f"{prompt:<50s} : ", end="")

or count, how many places are left for the number:

print(f"Enter the point ID of unknown point {i:<14d} : ", end="")

Upvotes: 1

PCM
PCM

Reputation: 3011

Use f-string literal to solve this

Spaces = ' ' * 50
print(f"Enter the point ID of unknown point : {i}{Spaces}")

Use {Spaces} wherever you want to add 50 spaces

Upvotes: 0

Related Questions