3

I want to change the length of the formatting in my code,

symbolWanted = input('Input symbol wanted for triangle: ')
baseLength = int(input('Input base length of triangle(odd number): '))
for i in range (1,baseLength,2):
   print(('{:^100}').format(i*symbolWanted))

is there a way to change the length of the format using a variable e.g.

print(('{:^baseLength}').format(i*symbolWanted))

Thank you.

3 Answers3

3

You can use variables in format strings:

print(('{:^{bl}}').format(i*symbolWanted, bl=baseLength))
Tim Pietzcker
  • 328,213
  • 58
  • 503
  • 561
0

You can use the advantage of Py >=3.6 with f-strings:

print(f"{i * symbolWanted:^{baseLength}}")
Charnel
  • 4,222
  • 2
  • 16
  • 28
-1

Try using print((':^{baseLength}').format()