2

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!

2
  • 2
    try this: f'{3:{N}b}' Commented Jan 14, 2022 at 5:16
  • @HIMANSHU PANDEY Thank you! Commented Jan 14, 2022 at 5:21

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.