I'm trying to convert numbers into binary strings with a specified length in python. In my function, the length N
is a variable, so I tried f'{3:Nb}'
, where 3 is just some random numbers. However, it returns ValueError: Invalid format specifier
. Is there a way I can include the variable in this string formatting mini-language? Thanks!
Asked
Active
Viewed 16 times
2

ZR-
- 809
- 1
- 4
- 12
-
2try this: `f'{3:{N}b}'` – HIMANSHU PANDEY Jan 14 '22 at 05:16
-
@HIMANSHU PANDEY Thank you! – ZR- Jan 14 '22 at 05:21