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.
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.
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')
python: efficient way to check for leading zeros in a bytes object - Stack Overflow
Need zeros before the number in int
How would you add zeros to the beginning of an Integer, such that there are 4 total digits in the number?
Leading zero in MicroPython tuple
How are you getting leading zeros as a number? Do you mean "02" instead?
In general leading zeros are not allowed in Python unless followed by a 'x' for hexadecimal, 'o' for octal, or 'b' for binary. E.g. "0x8F", "0b110011". It seems Micropython is allowing unmarked leading zeros, but they are invalid. Just don't type the leading zeros.
More on reddit.comWithout using zfill or format or any other obscure functions.
example:
turn 15 into 0015
or 234 into 0234
I noticed something odd about tuples. If a zero is leading an integer in your conditional value, MicroPython does not return either True OR False. Instead appears to do nothing.
Is this by design? I can see where this might cause unexpected behavior.
MicroPython v1.21.0 on 2023-10-06; Raspberry Pi Pico W with RP2040 Type "help()" for more information. >>> import time >>> now = time.localtime() >>> now (2023, 11, 14, 20, 21, 32, 1, 318) >>> now[6] == 1 #True True >>> now[6] == 01 #True? >>> now[6] == 02 #Obviously False >>> now[6] == 02 - 01 >>> now[6] == (02 - 01) >>> now[6] == (02.0 - 01.0) True >>> now[6] == (02.0 - 01) >>>
How are you getting leading zeros as a number? Do you mean "02" instead?
In general leading zeros are not allowed in Python unless followed by a 'x' for hexadecimal, 'o' for octal, or 'b' for binary. E.g. "0x8F", "0b110011". It seems Micropython is allowing unmarked leading zeros, but they are invalid. Just don't type the leading zeros.
This has nothing to do with tuples, the oddness is on how MicriPython deals with numbers with leading zeroes.
Like the other comment said, in normal Python leading zeroes are not allowed, Python 3 for instance gives this error:
SyntaxError: leading zeros in decimal integer literals are not permitted; use an 0o prefix for octal integers
But MicroPython seems to accept leading zeroes, although it has some weirdness related to it. From what I gathered, the weirdness seems to be related to what it shows in the execution window, though.
See this for example:
>>> 01 == 01
>>> x = (01 == 01)
>>> x
True
>>>
The first line should obviously show True, but the other lines show that the value is computed correctly, it's just not displayed.
So, just to show that what you're trying to do works:
>>> import time
>>> now = time.localtime()
>>> now
(2023, 11, 15, 13, 38, 5, 2, 319)
>>> if now[6] == 02: print('yes')
yes
As you can see, the last line does execute the print('yes'). So the only weirdness is that comparisons involving numbers with leading zeroes don't display the result when executed in that MicroPython shell.
Use lstrip:
>>> '00000010'.lstrip('0')
'10'
(strip removes both leading and trailing zeros.)
This messes up '0' (turning it into an empty string). There are several ways to fix this:
#1:
>>> re.sub(r'0+(.+)', r'\1', '000010')
'10'
>>> re.sub(r'0+(.+)', r'\1', '0')
'0'
#2:
>>> str(int('0000010'))
'10'
#3:
>>> s = '000010'
>>> s[:-1].lstrip('0') + s[-1]
just use the int() function, it will change the string into an integer and remove the zeros
my_str = '00000010'
my_int = int(my_str)
print(my_int)
output:
10