Add leading zeros into binary [duplicate] - python

This question already has answers here:
Convert to binary and keep leading zeros
(10 answers)
Closed 2 years ago.
Hi i would like to add leading zeros into binary to make it to have 8 digits. I've tried using zfill() but it doesnt seem to work and i have no idea how to use format() cause all the answers i've found are all keeping the 0b which i dont want that.
Here's what i've tried:
lenToBin = bin(payloadLength).replace("0b", "")
if payloadLength == 30 or payloadLength == 31:
binResult = lenToBin.zfill(3)
else:
binResult = lenToBin.zfill(2)

Is this what you want:
lenToBin = bin(payloadLength).replace("0b", "").zfill(8)

Related

How to split a number string using a comma in every 3rd digit from the right [duplicate]

This question already has answers here:
How to print a number using commas as thousands separators
(30 answers)
Add commas into number string [duplicate]
(11 answers)
What's the easiest way to add commas to an integer? [duplicate]
(4 answers)
Adding thousand separator while printing a number [duplicate]
(2 answers)
Closed 2 years ago.
A beginner here! Can I ask how to split a number string in every 3rd digit from the right and put a comma in between and do it again.
>>>num = '12550'
how can I make that into this
>>>12,550
You can use this neat trick:
num = 123456789
print ("{:,.2f}".format(num))
which outputs:
123,456,789.00
If you don't want the decimal places just use:
print ("{:,}".format(num))

Python: How do add leading zeros to a binary? When I try it adds them to the left of the 0b [duplicate]

This question already has answers here:
Convert to binary and keep leading zeros
(10 answers)
Converting integer to binary in python
(17 answers)
How to pad a numeric string with zeros to the right in Python?
(3 answers)
Closed 4 years ago.
In this example...
temp = 0b110
print temp
temp.str(temp).zfill(8)
print temp
the output is...
0b110
0000b110
How do I add the zeros so I see an eight bit binary output, 0b00000110?
If you're calling the bin function, or using a format string with #b, or whatever, the resulting string has 0b on the front, so it's too late to zero-fill, unless you want to do something hacky like pull it off, zero-fill to n-2 characters, then put it back on.
But if you just do the zero-filling before (or while) adding the prefix rather than after, it's easy. For example:
>>> temp = 0b110
>>> format(temp, '#010b')
'0b00000110'
The docs explain this in detail, but the short version is:
# means "alternate form", which gives me the 0b prefix
0 means zero-pad
10 means width of 10 (including the 0b)
b means binary

Python: How to convert float to string without `.0`? [duplicate]

This question already has answers here:
Formatting floats without trailing zeros
(21 answers)
Closed 6 years ago.
I want to extract telephone number from *.xls with module xlrd using Python, but it shows like this:'16753435903.0'.
Here's the code:
opensheet = openxls.sheet_by_name(u'sms')
sheetrows = opensheet.nrows
for rownum in range(1, sheetrows):
rowvalue = opensheet.row(rownum)
execinfo = ""
for colnum in range(1,5):
execinfo += "'"+str(rowvalue[colnum].value)+"',"
The key part:
str(rowvalue[colnum].value)
How can I get the well format telephone number without .0?
This might be a very simple workaround: convert the number to an int before converting it to a string?

When Rounding to nearest hundreds, how do I include 0s [duplicate]

This question already has answers here:
Rounding a number in Python but keeping ending zeros
(6 answers)
Closed 6 years ago.
So let's say I have this code:
num = 1.29283
round(num, 2)
That rounds to 1.29, but if I do this:
num = 1.30293
round(num, 2)
That rounds to 1.3. I want to know if there is a way to have it round to 1.30; I know it is the same number, but I need it to print 1.30.
You can use string formatting for this. A number in python does not have such a thing as trailing zeros. So your question only make sense for strings.
Example:
>>> num = 1.30293
>>> "{:.2f}".format(num)
'1.30'
The .2f says that this is a float (f) and that you want two digits after the point .2. Read more about string formatting here

Unwanted decimal in Python [duplicate]

This question already has answers here:
Formatting floats without trailing zeros
(21 answers)
Closed 8 years ago.
I have written a code that will add up the values in a tuple and calculate the average:
def average(values):
return sum(values[0:]) / len(values[0:])
However, I get an unwanted floating point, like 2.0 instead of 2. How do I eliminate this, but still manage to get the correct average should the average not be an integer?
You may try like this:
if (yournumber).is_integer():
print int(n)
else
print (n)

Categories