# String subs

**URL:** <https://rubytalk.org/t/string-subs/37098>\
**Category:** ruby-talk\
**Created:** [21 April 2007 21:40 UTC](https://rubytalk.org/t/string-subs/37098 "2007-04-21T21:40:35Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![nephish](https://avatars.discourse-cdn.com/v4/letter/n/8e7dd6/32.png) [@nephish](https://rubytalk.org/u/nephish)\
**Post date:** [21 April 2007 21:40 UTC](https://rubytalk.org/t/string-subs/37098/1 "2007-04-21T21:40:35Z")

</div>

lo there all,

i have found how i can put a variable inside a string without the #{} stuff  
using %s and so on. But i don't know how to do more than one.

for example

x = 5  
y = 10

puts "i have %s foo and %s bar" % don't know what goes here

i know, quite a beginner question, would appreciate any help

thanks  
sk

---

<div class="post-metadata">

**Author:** ![Roland\_Crosby](https://avatars.discourse-cdn.com/v4/letter/r/3bc359/32.png) [@Roland\_Crosby](https://rubytalk.org/u/Roland_Crosby)\
**Post date:** [21 April 2007 21:43 UTC](https://rubytalk.org/t/string-subs/37098/2 "2007-04-21T21:43:58Z")

</div>

From ri:

--------------------------------------------------------------- String#%  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;str % arg =\> new\_str

> **···**
>
> On Apr 21, 2007, at 5:40 PM, shawn bright wrote:
> 
> > lo there all,
> > 
> > i have found how i can put a variable inside a string without the #{} stuff  
> > using %s and so on. But i don't know how to do more than one.
> > 
> > for example
> > 
> > x = 5  
> > y = 10
> > 
> > puts "i have %s foo and %s bar" % don't know what goes here
> > 
> > i know, quite a beginner question, would appreciate any help
> > 
> > thanks  
> > sk
> 
> ------------------------------------------------------------------------  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Format---Uses \_str\_ as a format specification, and returns the  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;result of applying it to \_arg\_. If the format specification  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;contains more than one substitution, then \_arg\_ must be an +Array+  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;containing the values to be substituted. See +Kernel::sprintf+ for  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;details of the format string.
> 
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"%05d" % 123 #=\> "00123"  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"%-5s: %08x" % ["ID", self.id] #=\> "ID : 200e14d6"
> 
> So, it'd be:
> 
> puts "i have %s foo and %s bar" % [foo, bar]

---

<div class="post-metadata">

**Author:** ![nephish](https://avatars.discourse-cdn.com/v4/letter/n/8e7dd6/32.png) [@nephish](https://rubytalk.org/u/nephish)\
**Post date:** [21 April 2007 22:38 UTC](https://rubytalk.org/t/string-subs/37098/3 "2007-04-21T22:38:33Z")

</div>

cool, thanks very much  
sk

> **···**
>
> On 4/21/07, Roland Crosby \<roland.crosby@students.olin.edu\> wrote:
> 
> > On Apr 21, 2007, at 5:40 PM, shawn bright wrote:
> > 
> > \> lo there all,  
> > \>  
> > \> i have found how i can put a variable inside a string without the #  
> > \> {} stuff  
> > \> using %s and so on. But i don't know how to do more than one.  
> > \>  
> > \> for example  
> > \>  
> > \> x = 5  
> > \> y = 10  
> > \>  
> > \> puts "i have %s foo and %s bar" % don't know what goes here  
> > \>  
> > \> i know, quite a beginner question, would appreciate any help  
> > \>  
> > \> thanks  
> > \> sk  
> > \>
> > 
> > From ri:
> > 
> > --------------------------------------------------------------- String#%  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;str % arg =\> new\_str  
> > ------------------------------------------------------------------------  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Format---Uses \_str\_ as a format specification, and returns the  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;result of applying it to \_arg\_. If the format specification  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;contains more than one substitution, then \_arg\_ must be an +Array+  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;containing the values to be substituted. See +Kernel::sprintf+ for  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;details of the format string.
> > 
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"%05d" % 123 #=\> "00123"  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"%-5s: %08x" % ["ID", self.id] #=\> "ID : 200e14d6"
> > 
> > So, it'd be:
> > 
> > puts "i have %s foo and %s bar" % [foo, bar]
