5

I've got an algorithm that does this already, but the code is ugly and slow. I assume that there's a built-in or some function in the libs that give faster and more precise results.

What I want it where I can give a final number of bits, a string, list or deque (anything that can be cast to a string in the end).

func(totalBits, binaryInStringOrListForm) -> String or List with leading zeroes

The code I've already got is below:

 val = deque(["1", "0"])

     while len(val) < 8:

         val.appendleft("0")

By the way, deque is a type of object from collections that allows you to leftappend wherein you can add an item to the beginning of the list without having to shift everything over by one.

There are old answers to this but they're all Python 2 and the whole formatting thing has changed significantly.

EDIT: I think that this question is not a duplicate of the one linked as I was actually after leading zeroes as @Martijn Pieters has made me aware.

EDIT: Ok, it IS a duplicate of that one it seems. Sorry about the dupe.

LAST EDIT I SWEAR: I just noticed something. I keep getting values like 000b0110 when trying to make it one-byte long. This is opposed to 00000110 that I wanted. I'm just adding this so that if anyone else in the future looks up this question they'll find the correct thing. So all I've done is ignore the first two character of the string by using:

val = val[2::].rjust(8, '0')

I'm getting this because I'm turning a number INTO a binary number with "bin" which returns a string that has the "0b" (declaring it as binary) at the beginning.

ROMANIA_engineer
  • 54,432
  • 29
  • 203
  • 199
Frogboxe
  • 386
  • 4
  • 12

2 Answers2

12

There is a built-in, the str.ljust() method. It takes the target width and an optional fill character (defaulting to a space). You can use it to pad out a string with '0' characters:

val = val.ljust(8, '0')

There is also an equivalent str.rjust() method to add padding on the left.

However, if you want leading zeros (as opposed to trailing), I'd use the str.zfill() method as that takes any - or + prefix into account.

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
  • Thank you. This was what I was looking for. Cannot accept the answer though because stackoverflow giving a message saying: "You can accept an answer in 8 minutes" – Frogboxe Sep 29 '16 at 18:45
  • Oh, and yes, it was leading zeroes I needed. – Frogboxe Sep 29 '16 at 18:52
4

You can use the formatting language but this particular example is probably unnecessary:

>>> format('101', '<08')
'10100000'
>>> "{:<0{width}}".format('101', width=8)
'10100000'
AChampion
  • 29,683
  • 4
  • 59
  • 75
  • Not sure why this was downvoted, it is certainly not a wrong or unhelpful answer. Note that you shouldn't really use `str.format()` when you could just use the `format()` function: `format('101', '<08')` gives you the same results as the first option, without having to parse a string template. – Martijn Pieters Sep 29 '16 at 18:50
  • I agree. It's good to have two methods anyway too. I've upvoted too. – Frogboxe Sep 29 '16 at 18:57
  • Good point, changed... – AChampion Sep 29 '16 at 19:42