String formatting with left zero padding - python

I would like to format a string with dynamically changing left zero padding depending on the value input to the string. Something like this:
value = 123
n=6
print("{value:0n}".format(value=value, n=n))
'000123'
But I can't quite get it to work.

Try str.zfill:
print(str(value).zfill(n))
Output:
000123

try
f'%0{n}d' % value # python >= 3.6

Related

hex to int python but get a letter in response

This is pretty well documented but I keep getting output that doesn't make sense. I have a hex value that looks like
\x00\x00\x00\x00\x00\x01\x86\xa0
but I get
>>> b'\x00\x00\x00\x00\x00\x01\x86\xa0'.hex()
'00000000000186a0'
I am expecting a int or at least a readable number. I assume I am using the wrong function.
Advice?
You need to add a base value of 16
For Example
hex_string = "a1"
integer = int(hex_string, 16)
print(integer)
The output of this will be 161
Try this out
Then try this
hex_bytes = b'\x12\x34'
integer= int.from_bytes(hex_bytes,byteorder='big')
print(integer)

Keep zero digit save while converting string to integer in python

I am converting a string into integer using int function and it is working fine but i want to keep save zero digit that are at the start of the string.
string_value = '0123'
print(int(string_value))
result is 123
How can i format output 0123 as in integer type value not in string.
You can't, but if you want to put 0's (zero padding) at the beginning of your number, this is the way to do it.
"{:04}".format(123)
# '0123'
"{:05}".format(123)
# '00123'
Like every one said you can try above answers or the following :
string_value = '0123'
int_no = int(string_value)
print("%04d" % int_no)
print(string_value.zfill(4))
Both will give same answer
Impossible, you cannot get an integer value of 0123.
You should change your mind, you do not actually need 0123 in integer, but you need to keep zero when displaying it. So the question should change to how to format output.

How to parse values like \x00, \x95x95?

I have String which is like:
\x00\x00\x01\x90\x27 and on.
How can I parse this value to a simple long value?
Like, what is the long value of \x00? 0 or 00?
I am assuming that you thing your data is character...
So using python 3
a=b'\x00\x00\x01\x90'
b=b.decode('utf-8')
If your binary data has some 'nasty' i.e. bad characters this will fail - but adding an option like errors='ignore' or errors='replace' will assist you unhanding those issues.
Your question is not very clear so I am making some assumptions here. If you need fixed length conversions then you can use standard structs module.
For example:
In [11]: unpack('>i', '\x00\x01\x90\x27')
Out[11]: (102439,)
if you need variable length string then something like this will work (for unsigned and watch for endianness):
s = '\x00\x00\x01\x90\x27'
i = 0
for c in s:
i <<= 8
i |= ord(c)
print i

Remove the 0b in binary

I am trying to convert a binary number I have to take out the 0b string out.
I understand how to get a bin number
x = 17
print(bin(17))
'0b10001'
but I want to take the 0b in the string out and I am having some issues with doing this. This is going to be within a function returning a binary number without the 0b.
Use slice operation to remove the first two characters.
In [1]: x = 17
In [2]: y = bin(x)[2:]
In [3]: y
Out[3]: '10001'
use python string slice operation.
a = bin(17)
b = bin(17)[2:]
to format this to 8-bits use zfill.
c = b.zfill(8)
It's easy just make this function:
def f(n):print('{:0b}'.format(n))
f(17)
>>> 10001
format(17, 'b')
>>> '10001'
Use the format() builtin. It also works for hexadecimal, simply replace 'b' with 'x'.
https://docs.python.org/3/library/functions.html#format
bin(n).replace("0b", "")
This one is using replace
Where n is the provided decimal
with Python 3.6 you can use f-strings
print( f'{x:b}' )
'10001'
I do not know why nobody suggested using lstrip.
integer = 17
bit_string = bin(integer)
final = bit_string.lstrip('-0b') # minus to also handle negations
print(final) # prints 10001
inhexa=(hexanum.get()) # gets the hexa value
dec = int(inhexa,16) #changes the base ensures conversion into base 16 to interger
b=bin(dec)[2:] #converts int/dec into binary and shows string except first two digits
print (bin(int(input().strip()))[2:])
Pythonic way to solve. ;)

Adding '0' to binary

How can i add number of '0' values to the left side of an existing binary type?
im getting the binary type by using the following:
binary=bin(int(symbol))
where symbol is an int.
is there any way of doing that?
i want the result to be a string.
There is no binary type, the result of bin() is a string. Here is how you can add additional zeroes to the end of a string:
>>> bin(11)
'0b1011'
>>> bin(11) + '0000'
'0b10110000'
Since it sounds like you want to add the zeroes on the left side, I'm assuming that you are doing this so that the resulting strings are the same length regardless of the value for symbol.
One good way to do this is to use str.format() instead of the bin() function, here is an example where there are always eight digits in the resulting string, and the 0b prefix is still there as if you had used bin():
>>> '0b{0:0>8b}'.format(3)
'0b00000011'
>>> '0b{0:0>8b}'.format(11)
'0b00001011'
Not exactly sure what you want, adding '0' to where.
print bin(1<<8)
print bin(1).zfill(8).replace("b", "")
Hope that helps. ~Ben

Categories

Resources