Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial


What does this mean?

So I was watching a couple of people and I've seen one of em' do something like this:

print("blah blah blah blah %s" %  **variable**)

what does %s and % mean??

1 Answer

Bhavesh Hirani
Bhavesh Hirani
7,381 Points

They are used for formatting strings. %s acts a placeholder for a string while %d acts as a placeholder for a number. Their associated values are passed in via a tuple using the % operator.

It is very handy when concatenating a number to a string.


friend1 = "Sam"
friend2 = "John"
friend3 = "Peter"
integer = 3

print("My friends are " + friend1 + ", " + friend2 + " and " + friend3) #My friends are Sam, John and Peter

print("My friends are %s, %s and %s" % (friend1, friend2, friend3)) #My friends are Sam, John and Peter

print("My friends are " + friend1 + ", " + friend2 + " and " + friend3 + ". I have " + integer + "friends.") # Error

print("My friends are %s, %s and %s. I have %d friends" % (friend1, friend2, friend3, integer)) # My friends are Sam, John and Peter. I have 3 friends.

@Bhavesh Hirani, I've been looking for an answer for weeks, lmao.

So it's more of a shortcut kind of. well, thank you, mister!