-1

Error message:

alarm = f"{alarm_hour}:{alarm_minute:02}:{alarm_am_pm}"

ValueError: '=' alignment not allowed in string format specifier

I don't know why this code is giving the error as I have used the same before in the 's program and it's working fine. Here's the code.

TIME = f"{current_hour}:{current_minute:02}:{current_second} {am_pm}"

But whenever I remove :02 from alarm_minute it works as I wanted to show number in 2 digits. I'm confused what's wrong with the code. Both are almost identical but one is giving error other's don't.

khelwood
  • 55,782
  • 14
  • 81
  • 108
codework
  • 25
  • 1
  • 9
  • The error message is a bit misleading, but the problem is the `:02` format. The leading `0` is only valid for numeric types, and causes an error with strings. Change it to `:2`. – Tom Karzes May 03 '21 at 09:04

1 Answers1

0

Assuming you want to have the number formated as a two digit with leading 0 when its a single digit you can pass this with the letter d to let the fstring know you want to format as 2 digits leaded by 0. you can do similar with floats to print to x many decimal places.

num = 3
price = 4.9
print(f"{num:02d}, {price:.2f}")

OUTPUT

03, 4.90
Chris Doyle
  • 10,703
  • 2
  • 23
  • 42