# Printf inside a string

**URL:** <https://www.ruby-forum.com/t/printf-inside-a-string/87309>\
**Category:** Ruby\
**Created:** [March 1, 2007, 4:39pm UTC](https://www.ruby-forum.com/t/printf-inside-a-string/87309 "2007-03-01T16:39:10Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![bodikp](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/e79b87/32.png) [@bodikp](https://www.ruby-forum.com/u/bodikp)\
**Post date:** [March 1, 2007, 4:39pm UTC](https://www.ruby-forum.com/t/printf-inside-a-string/87309/1 "2007-03-01T16:39:10Z")

</div>

Hi,  
This is a pretty simple question. I need to do a “printf” of some  
numbers inside a string. I just can’t figure out how to express a long  
string that has that “printf” inside it, because the printf statement  
itself has quotes, too. I just need the number to end up being  
zero-padded to 3 characters. So, for “3,” I want “003.”

number = “3”

puts “stuff, printf “%.3d\n”, #{number}”  
…compile error…

Thanks,  
Peter

---

<div class="post-metadata">

**Author:** ![bodikp](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/e79b87/32.png) [@bodikp](https://www.ruby-forum.com/u/bodikp)\
**Post date:** [March 1, 2007, 4:45pm UTC](https://www.ruby-forum.com/t/printf-inside-a-string/87309/2 "2007-03-01T16:45:51Z")

</div>

On Mar 1, 2007, at 9:39 AM, Peter B. wrote:

> Hi,  
> This is a pretty simple question. I need to do a “printf” of some  
> numbers inside a string. I just can’t figure out how to express a long  
> string that has that “printf” inside it, because the printf statement  
> itself has quotes, too. I just need the number to end up being  
> zero-padded to 3 characters. So, for “3,” I want “003.”
> 
> number = “3”
> 
> puts “stuff, printf “%.3d\n”, #{number}”

> > printf “stuff, %.3d\n”, number  
> > stuff, 003  
> > =\> nil

Hope that helps.

James Edward G. II

---

<div class="post-metadata">

**Author:** ![bodikp](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/e79b87/32.png) [@bodikp](https://www.ruby-forum.com/u/bodikp)\
**Post date:** [March 1, 2007, 5:07pm UTC](https://www.ruby-forum.com/t/printf-inside-a-string/87309/3 "2007-03-01T17:07:53Z")

</div>

On Fri, 2 Mar 2007, James Edward G. II wrote:

> > puts “stuff, printf “%.3d\n”, #{number}”
> 
> > > printf “stuff, %.3d\n”, number  
> > > stuff, 003  
> > > =\> nil
> 
> Hope that helps.

alternatively

puts “stuff, #{ ‘%.3d’ % number }”

-a

---

<div class="post-metadata">

**Author:** ![bodikp](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/e79b87/32.png) [@bodikp](https://www.ruby-forum.com/u/bodikp)\
**Post date:** [March 1, 2007, 5:17pm UTC](https://www.ruby-forum.com/t/printf-inside-a-string/87309/4 "2007-03-01T17:17:55Z")

</div>

On Fri, Mar 02, 2007 at 12:39:13AM +0900, Peter B. wrote:

> This is a pretty simple question. I need to do a “printf” of some  
> numbers inside a string. I just can’t figure out how to express a long  
> string that has that “printf” inside it, because the printf statement  
> itself has quotes, too.

I think what you were trying to do is:

number = “3”  
puts “stuff, #{sprintf “%.3d”, number}”

Note that sprintf(…) is like printf(…) but returns the result as a  
string  
value, rather than sending it to stdout. Then #{} lets you insert an  
arbitary expression inside another string.

But as pointed out by others, you can do this in one go as

printf “stuff, %.3d\n”, number

HTH,

Brian.

---

<div class="post-metadata">

**Author:** ![bodikp](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/e79b87/32.png) [@bodikp](https://www.ruby-forum.com/u/bodikp)\
**Post date:** [March 1, 2007, 4:50pm UTC](https://www.ruby-forum.com/t/printf-inside-a-string/87309/5 "2007-03-01T16:50:28Z")

</div>

Peter B. wrote:

> Hi,  
> This is a pretty simple question. I need to do a “printf” of some  
> numbers inside a string. I just can’t figure out how to express a long  
> string that has that “printf” inside it, because the printf statement  
> itself has quotes, too. I just need the number to end up being  
> zero-padded to 3 characters. So, for “3,” I want “003.”
> 
> number = “3”
> 
> puts “stuff, printf “%.3d\n”, #{number}”  
> …compile error…
> 
> Thanks,  
> Peter

Hi Peter,

I don’t quite get what are you trying to do, but I advice to do that:

printf(“stuff, %.3d\n”,number)

That should be all.

Alin.

---

<div class="post-metadata">

**Author:** ![bodikp](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/e79b87/32.png) [@bodikp](https://www.ruby-forum.com/u/bodikp)\
**Post date:** [March 1, 2007, 6:00pm UTC](https://www.ruby-forum.com/t/printf-inside-a-string/87309/6 "2007-03-01T18:00:06Z")

</div>

Brian C. wrote:

> On Fri, Mar 02, 2007 at 12:39:13AM +0900, Peter B. wrote:
> 
> > This is a pretty simple question. I need to do a “printf” of some  
> > numbers inside a string. I just can’t figure out how to express a long  
> > string that has that “printf” inside it, because the printf statement  
> > itself has quotes, too.
> 
> I think what you were trying to do is:
> 
> number = “3”  
> puts “stuff, #{sprintf “%.3d”, number}”
> 
> Note that sprintf(…) is like printf(…) but returns the result as a  
> string  
> value, rather than sending it to stdout. Then #{} lets you insert an  
> arbitary expression inside another string.
> 
> But as pointed out by others, you can do this in one go as
> 
> printf “stuff, %.3d\n”, number
> 
> HTH,
> 
> Brian.

Thanks to all you guys. I’ll probably use sprintf, actually, now that  
I’m clear about how it’s used.

Cheers.
