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!
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

Jassim Alhatem
20,883 PointsWhat 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
7,381 PointsThey 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.
e.g
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.
Jassim Alhatem
20,883 PointsJassim Alhatem
20,883 Points@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!