https://wikidocs.net/21#_2 Solving exercise #5.
I learned that there are two ways to format the string (how to use format, how to use f'), but I wonder why only f' doesn't work.
i=7
while i > 0:
if (i % 2):
temp='*' * i
print(f'{temp:^7}')
i=i - 1
This code and
i=7
while i > 0:
if (i % 2):
temp='*' * i
print(f'{temp:^7}')
i=i - 1
works.
I thought the temp was unnecessary, so I modified it as below.
i=7
while i > 0:
if (i % 2):
print(f'{('*' * i):^7}')
i=i - 1
File "", line 3 SyntaxError: f-string: mismatched '(', '{', or '['
Isn't the f' method similar to the format?
string
I think you made a mistake using quotation marks in print(f'{('*' * i:^7}')
.
Why don't you write print(f'{("*" * i):^7}')
?
If you didn't understand my answer well, please leave a comment.
© 2024 OneMinuteCode. All rights reserved.