# Equivalent of Python zfill?

**URL:** <https://www.ruby-forum.com/t/equivalent-of-python-zfill/61886>\
**Category:** Ruby\
**Created:** [May 29, 2006, 11:51am UTC](https://www.ruby-forum.com/t/equivalent-of-python-zfill/61886 "2006-05-29T11:51:35Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![splattael](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/858c86/32.png) [@splattael](https://www.ruby-forum.com/u/splattael)\
**Post date:** [May 29, 2006, 11:51am UTC](https://www.ruby-forum.com/t/equivalent-of-python-zfill/61886/1 "2006-05-29T11:51:35Z")

</div>

Hello all,

I need a function which takes a number (x) and outputs a string which  
contains x zeroes. e.g.

foo(3) = “000”  
foo(5) = “00000”

etc.

in Python this function is ‘’.zfill(5)  
another possibility would be 5\*‘0’

Equivalents in Ruby? I would be mainly interested in a zfill equivalent  
since that is faster that join or 5\* imho.

Thx  
Peter

---

<div class="post-metadata">

**Author:** ![splattael](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/858c86/32.png) [@splattael](https://www.ruby-forum.com/u/splattael)\
**Post date:** [May 29, 2006, 12:13pm UTC](https://www.ruby-forum.com/t/equivalent-of-python-zfill/61886/2 "2006-05-29T12:13:04Z")

</div>

2006/5/29, Peter S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> in Python this function is ‘’.zfill(5)  
> another possibility would be 5\*‘0’
> 
> Equivalents in Ruby? I would be mainly interested in a zfill equivalent  
> since that is faster that join or 5\* imho.

> > “0” \* 7  
> > =\> “0000000”

Kind regards

robert

---

<div class="post-metadata">

**Author:** ![splattael](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/858c86/32.png) [@splattael](https://www.ruby-forum.com/u/splattael)\
**Post date:** [May 29, 2006, 12:19pm UTC](https://www.ruby-forum.com/t/equivalent-of-python-zfill/61886/3 "2006-05-29T12:19:16Z")

</div>

Robert,

> > > “0” \* 7  
> > > =\> “0000000”

Thanks, i am using this at the moment, just wanted to know if there is  
something faster - in Python zfill is faster than “0” \* 7. I am just  
asking because i am using this a _lot_ of times, so if there is an at  
least slightly faster alternative ,i should probably consider it. But if  
“0”\*7 is the ‘normal’ way of doing this, it’s OK with me.

Cheers,  
Peter

---

<div class="post-metadata">

**Author:** ![splattael](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/858c86/32.png) [@splattael](https://www.ruby-forum.com/u/splattael)\
**Post date:** [May 29, 2006, 12:56pm UTC](https://www.ruby-forum.com/t/equivalent-of-python-zfill/61886/4 "2006-05-29T12:56:02Z")

</div>

On 5/29/06, Peter S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Robert,
> 
> > > > “0” \* 7  
> > > > =\> “0000000”
> 
> Thanks, i am using this at the moment, just wanted to know if there is  
> something faster - in Python zfill is faster than “0” \* 7. I am just  
> asking because i am using this a _lot_ of times, so if there is an at  
> least slightly faster alternative ,i should probably consider it. But if  
> “0”\*7 is the ‘normal’ way of doing this, it’s OK with me.

It looks like zfill is primarily used for padding numbers, in which  
case I’d use sprintf:

sprintf(“%07d”, 6) or “%07d” % 6  
=\> “0000006”

Not sure how well it benchmarks - probably better if it maps to the C  
sprintf function. But you’re just looking for zeros without padding an  
existing number? You could always pad out 0 to one less then what you  
want, but I guess that would be overcomplicating things even if it was  
a little faster. 🙂

-Pawel

---

<div class="post-metadata">

**Author:** ![splattael](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/858c86/32.png) [@splattael](https://www.ruby-forum.com/u/splattael)\
**Post date:** [May 29, 2006, 1:20pm UTC](https://www.ruby-forum.com/t/equivalent-of-python-zfill/61886/5 "2006-05-29T13:20:13Z")

</div>

On Mon, May 29, 2006, Pawel S. wrote:

> It looks like zfill is primarily used for padding numbers, in which  
> case I’d use sprintf:
> 
> sprintf("%07d", 6) or “%07d” % 6  
> =\> “0000006”

You can also use String#rjust:

irb(main):001:0\> ‘8’.rjust(4, ‘0’)  
=\> “0008”

If you just want a few string of 0 of different lengthes, you can also  
use a precomputed array instead of recomputing ‘0’\*4 every time

ie.

ZEROES = %w(x 0 00 000 0000)  
str\_with\_4zeroes = ZEROES[4]

just my 2c

---

<div class="post-metadata">

**Author:** ![splattael](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/858c86/32.png) [@splattael](https://www.ruby-forum.com/u/splattael)\
**Post date:** [May 29, 2006, 1:29pm UTC](https://www.ruby-forum.com/t/equivalent-of-python-zfill/61886/6 "2006-05-29T13:29:22Z")

</div>

ZEROES = Array.new(100){|i|“0”\*i}

or

ZEROES = Hash.new{|k,v|k[v]=“0”\*v}

j`ey http://www.eachmapinject.com c,r=0,32;while r!=0;((c+=1)>31)?(c=0;r-=1; puts):print(c<r ?" ":~c&r!=0?"`":" #")end
