12

I have a binary string say '01110000', and I want to return the number of leading zeros in front without writing a forloop. Does anyone have any idea on how to do that? Preferably a way that also returns 0 if the string immediately starts with a '1'

1
  • "binary string" doesn't really make sense. That's a text string that happens to represent a number written in base-2. Commented Jan 24, 2013 at 20:20

8 Answers 8

13

Here is another way:

In [36]: s = '01110000'

In [37]: len(s) - len(s.lstrip('0'))
Out[37]: 1

It differs from the other solutions in that it actually counts the leading zeroes instead of finding the first 1. This makes it a little bit more general, although for your specific problem that doesn't matter.

Sign up to request clarification or add additional context in comments.

2 Comments

This is the only answer that works for me to find how many # a markdown line begins with to denote a header.
I used the idea behind this solution to determine if there is any leading zeros in a random string. However, it would not distinguish the zero in '0' as a single digit (not a leading zero) vs. the zero in '0234' as a leading zero. Both input strings, '0' or '0234' will return 1.
12

If you're really sure it's a "binary string":

input = '01110000'
zeroes = input.index('1')

Update: it breaks when there's nothing but "leading" zeroes

An alternate form that handles the all-zeroes case.

zeroes = (input+'1').index('1')

1 Comment

+1, that's even simpler than mine :) you could append '1' to the string to make sure it always contains a one.
9

A simple one-liner:

x = '01110000'
leading_zeros = len(x.split('1', 1)[0])

This partitions the string into everything up to the first '1' and the rest after it, then counts the length of the prefix. The second argument to split is just an optimization and represents the number of splits to perform, meaning the function will stop after it found the first '1' instead of splitting it on all occurences. You could just use x.split('1')[0] if performance doesn't matter.

1 Comment

+1 This is probably the most straightforward way to deal with a string of all zeroes.
4

I'd use:

s = '00001010'
sum(1 for _ in itertools.takewhile('0'.__eq__, s))

Rather pythonic, works in the general case, for example on the empty string and non-binary strings, and can handle strings of any length (or even iterators).

Comments

3

If you know it's only 0 or 1:

x.find(1)

(will return -1 if all zeros; you may or may not want that behavior)

Comments

2

If you don't know which number would be next to zeros i.e. "1" in this case, and you just want to check if there are leading zeros, you can convert to int and back and compare the two.

"0012300" == str(int("0012300"))

Comments

0

How about re module?

a = re.search('(?!0)', data)

then a.start() is the position.

Comments

0

I'm using has_leading_zero = re.match(r'0\d+', str(data)) as a solution that accepts any number and treats 0 as a valid number without a leading zero

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.