1

How can I add space as parameter in string in python For example, If I write:

s="test"
print(f" {s:< 5}{s}") 

Then

test     test

I want something that

w=5
printf(f" {s:<f'{w}'}{s}")
2
  • 2
    print(f" {s:<{w}}{s}") ? Commented Apr 15, 2020 at 7:41
  • Also you mean print instead of printf? Commented Apr 15, 2020 at 7:41

3 Answers 3

2

Try this:

s = "test"
w = 5
print(f" {s:<{w}}{s}")

This should produce the same result as:

print(f" {s:<5}{s}")
Sign up to request clarification or add additional context in comments.

Comments

1

If I have well understood, you want to print w spaces. You could do something like : print(f"{s:<{w}}{s}") Else, there is also the method ljust : print(s.ljust(w, " ")+s)

Comments

1

Instead Try This: print(f"{s:<{w}{s}")

Printf() is nothing in python until you define function named printf() manually. And You can't initialise format specifier inside format specifier.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.