Why doesn't f'blar blar' only work when formatting python strings?

Asked 2 years ago, Updated 2 years ago, 40 views

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

2022-09-22 18:54

1 Answers

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.


2022-09-22 18:54

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.