What is the most pythonic way to pad a numeric string with zeroes to the left, i.e., so the numeric string has a specific length? Str.zfill is specifically intended to do this: '1'.zfill(4) '0001'.
What is the nearly all Pythonic way to pad a numeric line with zeroes to the still left, i.age., so the numeric chain has a particular length?
kmario2321.7k5 platinum badges72 metallic badges83 bronze badges
Faisal
14 Answers
Strings:
And for numbers:
Chain formatting documentation.
ShadowRanger67.6k6 silver badges70 metallic badges108 bronze badges
Harley HolcombeHarley Holcombe120k14 money badges64 gold badges62 bronze badges
Just use the rjust technique of the chain item.
This illustration will create a line of 10 people longer, padding as required.
twasbrillig7,2195 gold badges28 sterling silver badges56 bronze badges
Paul N. EdenPaul D. Eden10.8k16 gold badges50 metallic badges59 bronze badges
Besides
zfill
, you can make use of general string format:Paperwork for thread format and f-strings.
Konrad RudolphKonrad Rudolph410k103 money badges805 metallic badges1048 bronze badges
Victor BarrantesVictor Barrantes1,8582 money badges15 silver badges12 bronze badges
Cees TimmermanCees Timmerman9,0033 yellow metal badges57 magic badges87 bronze badges
str(n).zfill(width)
will work withthread
beds,int
t,float
s i9000. and is definitely Python 2.xand 3.xcompatible:JohnsywebJohnsyweb
102k18 precious metal badges148 silver precious metal badges214 bronze badges
For the ones who came right here to realize and not really simply a quick solution.I do these specifically for time guitar strings:
'0' icons what to change with the '2' padding heroes, the default is usually an clear room
'gt;' emblems allign all the 2 '0' character to the still left of the chain
':' signs the formatspec
elad sterling silverelad sterling silver4,7002 silver badges30 magic badges47 bronze badges
Discover the printing paperwork for all the interesting details!
Revise for Python 3.x (7.5 decades later on)
That final series should today become:
We.elizabeth.
Peter RowellPhilip Rowellprint out
is definitely now a function, not really a statement. Note that I still prefer the Old Collegeprintf
style because, IMNSHO, it states better, and because, um, I've been using that notation since Jan, 1980. Something. outdated canines. something something. brand-new techniques.15.9k2 silver badges41 metallic badges61 bronze badges
What will be the almost all pythonic way to pad a numeric chain with zeroes to the left, i.y., so the numeric line has a specific size?
str.zfill
is specifically designed to do this:Take note that it is usually specifically intended to handle numeric strings as asked for, and moves a
+
or-
to the beginning of the string:Here's the help on
str.zfill
:Functionality
This can be furthermore the most performant of alternate strategies:
To greatest compare apples to pears for the
%
technique (take note it is actually slowly), which will otherwise pre-calculate:Execution
With a little looking, I found the execution of the
zfill
technique inItems/stringlib/transmogrify.l
:Let's walk through this G program code.
It very first parses the debate positionally, signifying it doesn't allow keyword disputes:
It then bank checks if it's the same duration or longer, in which situation it results the thread.
zfill
phone callspad
(thispad
functionality is furthermore known as byljust
,rjust
, andmiddle
as well). This basically duplicates the items into a fresh chain and fills in the padding.After calling
pad
,zfill
goes any initially preceding+
or-
to the beginning of the chain.Notice that for the first string to really become numeric is usually not required:
Aaron Area♦Aaron Corridor194k55 magic badges313 magic badges269 bronze badges
SaadSaad1,2991 money badge16 silver badges23 bronze badges
A-B-B25.6k6 gold badges70 metallic badges74 bronze badges
user221014
Quick timing assessment:
I've produced different lab tests of various reps. The distinctions are not massive, but in all testing, the
Simon SteinbergerSimon Steinbergerzfill
alternative was quickest.4,3111 gold badge31 sterling silver badges73 bronze badges
Another approach would be to make use of a checklist knowledge with a condition checking for lengths. Below is certainly a demonstration:
kmario23kmario2321.7k5 magic badges72 silver badges83 bronze badges
You could furthermore do it again '0', prepend it to
Danrastr(n)
and obtain the rightmost width cut. Quick and dirty little reflection.5,1691 gold badge43 silver precious metal badges96 bronze badges
J LacarL Lacar
securedby Vamsi PrabhalaAugust 30 '16 at 13:56
Thank you for your curiosity in this query. Because it offers fascinated low-quality or junk e-mail solutions that experienced to end up being removed, posting an answer now needs 10 reputation on this web site (the organization bonus will not rely).
Would you like to answer one of these unanswered questions rather?
Would you like to answer one of these unanswered questions rather?