I'm given a hexadecimal number in string form with a leading '0x' that may contain 1-8 digits, but I need to pad the number with zeros so that it always has 8 digits (10 characters including the '0. Formatted output in three ways: the string methods ljust, rjust, center, format. '0', If the width field is preceded by a zero ('0') character, sign-aware zero-padding.
I'meters attempting to transform an integer to binary making use of the bin functionality in Python. Nevertheless, it continually eliminates the top zeros, which I really need, such that the outcome is generally 8-bit:
Example:
Is certainly there a method of carrying out this?
Peter Mortensen
14.2k19 yellow metal badges88 silver badges114 bronze badges
Niels HønderbækNiels S i9000ønderbæk1,2134 platinum badges22 sterling silver badges38 bronze badges
8 Answers
Use the
format
functionality:The
format
function basically formats the insight pursuing the File format Specification mini language. The#
can make the format include the0b
prefix, and the010
dimension formats the result to fit in 10 heroes width, with0
padding; 2 heroes for the0b
prefix, the other 8 for the binary digits.This is definitely the almost all compact and direct option.
If you are putting the outcome in a larger string, make use of an formatted string literal (3.6+) or make use of
str.format
and put the 2nd discussion for theformat
functionality after the digestive tract of the placeholder:.
:As it occurs, also for just format a individual value (therefore without putting the result in a larger chain), making use of a formatted string literal can be faster than using
format
:But I'd make use of that just if overall performance in a tight loop matters, as
format(.)
communicates the intention better.If you did not would like the
Martijn Pieters♦Martijn Pieters0b
prefix, merely fall the#
and modify the size of the industry:741k156 gold badges2660 metallic badges2400 bronze badges
Discover: Format Specification Mini-Language
Notice for Python 2.6 or older, you cannot leave out the positional discussion identifier before
Josselin:
, therefore use1,6932 yellow metal badges15 metallic badges24 bronze badges
bwbrowningbwbrowning2,5574 yellow metal badges24 sterling silver badges31 bronze badges
rekinyzrekinyz4,9851 magic badge16 sterling silver badges26 bronze badges
You can make use of the chain formatting small vocabulary:
Demonstration:
Output:
EDIT:structured on @Martijn Pieters idea
Philip VaroPhilip Varo7,2253 platinum badges36 gold badges55 bronze badges
Occasionally you simply need a simple one liner:
Python 3
TagMark
Muds
3,1714 silver badges18 magic badges36 bronze badges
AdamAdam
Will16.9k10 magic badges61 silver precious metal badges83 bronze badges
DaahirDaahir
You can use zfill:
designs:
I like this remedy, as it helps not only when outputting the number, but when you need to give it to a adjustable. e.h. times = str(datetime.time.today.30 days).zfill(2) will return x as '02' for the month of feb.
Anshul GargAnshul Garg