How to pass string format as a variable to an f-string
you should to put the format_string
as variable
temp = f'{i:{format_string}}' + temp
the next code after :
is not parsed as variable until you clearly indicate.
And thank @timpietzcker for the link to the docs: formatted-string-literals
You need to keep the alignment and padding tokens separate from each other:
def display_pattern(n):
padding = 4
align = ">"
temp = ''
for i in range(1, n + 1):
temp = f'{i:{align}{padding}}' + temp
print(temp)
EDIT:
I think this isn't quite correct. I've done some testing and the following works as well:
def display_pattern(n):
align = ">4"
temp = ''
for i in range(1, n + 1):
temp = f'{i:{align}}' + temp
print(temp)
So I can't really say why your method wouldn't work...