# Sprintf percent sign problem

**URL:** <https://www.ruby-forum.com/t/sprintf-percent-sign-problem/119173>\
**Category:** Ruby\
**Created:** [November 4, 2007, 9:38am UTC](https://www.ruby-forum.com/t/sprintf-percent-sign-problem/119173 "2007-11-04T09:38:43Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Efthimios\_Bothos](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/5f9b8f/32.png) [@Efthimios\_Bothos](https://www.ruby-forum.com/u/Efthimios_Bothos)\
**Post date:** [November 4, 2007, 9:38am UTC](https://www.ruby-forum.com/t/sprintf-percent-sign-problem/119173/1 "2007-11-04T09:38:43Z")

</div>

Hi,  
I am trying to format some text using sprintf. The text itself  
contains the percent sign (%).

When I use it without any escape character, as expected ruby throws  
error (unkonown format sequence).

When I use the “%” sequence ruby complaints when the “%” is  
somewhere in the text. When it is at the end it works but I don’t want  
it there.

When I use the “%%” sequence, I get the exact sequence as output ("%%"  
is printed!!)… \

Has anyone observed this behavior? Am I doing something wrong or it is  
a ruby bug?

Thanks in advance,  
Efthimios.

---

<div class="post-metadata">

**Author:** ![Efthimios\_Bothos](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/5f9b8f/32.png) [@Efthimios\_Bothos](https://www.ruby-forum.com/u/Efthimios_Bothos)\
**Post date:** [November 4, 2007, 10:08am UTC](https://www.ruby-forum.com/t/sprintf-percent-sign-problem/119173/2 "2007-11-04T10:08:25Z")

</div>

Efthimios Bothos wrote:

> Hi,  
> I am trying to format some text using sprintf. The text itself  
> contains the percent sign (%).
> 
> When I use it without any escape character, as expected ruby throws  
> error (unkonown format sequence).

I’m not seeing that:

str = “hello%world”  
new\_str = sprintf(’%20s’, str)  
puts new\_str

–output:–  
hello%world

It’s always baffling to me why a poster doesn’t just post a simplified  
example along with the error.

---

<div class="post-metadata">

**Author:** ![Efthimios\_Bothos](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/5f9b8f/32.png) [@Efthimios\_Bothos](https://www.ruby-forum.com/u/Efthimios_Bothos)\
**Post date:** [November 4, 2007, 8:18pm UTC](https://www.ruby-forum.com/t/sprintf-percent-sign-problem/119173/3 "2007-11-04T20:18:52Z")

</div>

> It’s always baffling to me why a poster doesn’t just post a simplified  
> example along with the error.

If they knew to provide tests, they wouldn’t be asking syntax  
questions in the first place. That’s an experienced programmer’s rule,  
and syntax questions usually come from newbies.

It is a very good idea to provide test cases in responses, though.  
Apart from anything else, whenever I do that I go into IRB and half  
the time find out something more interesting than whatever I was going  
to say.

–  
Giles B.

Blog: [http://gilesbowkett.blogspot.com](http://gilesbowkett.blogspot.com)  
Portfolio: [http://www.gilesgoatboy.org](http://www.gilesgoatboy.org)  
Tumblelog: [http://giles.tumblr.com/](http://giles.tumblr.com/)
