Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

changing .format length in python 3

Tags:

python

format

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.

like image 697
PeterOFN - Gaming Avatar asked Nov 21 '25 06:11

PeterOFN - Gaming


1 Answers

You can use variables in format strings:

print(('{:^{bl}}').format(i*symbolWanted, bl=baseLength))
like image 90
Tim Pietzcker Avatar answered Nov 22 '25 20:11

Tim Pietzcker



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!