How does print ("hey %s there" % "you") gives output as hey you there? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

How does print ("hey %s there" % "you") gives output as hey you there?

What's with that percentage sign?

17th Jan 2017, 3:52 AM
Shin Rawat
Shin Rawat - avatar
5 Answers
+ 7
Someone else will probably be able to explain it more correctly, but %s is basically a place holder for a string. I know for C, C++ and Java's printf functions and method there are others like %d which is used for integer values and %f for floating point numbers. For example in Java System.out.print ("Hi " + 123) and System.out.printf ("%s %d", "Hi", 123) would both output Hi 123. Sorry for the poor explanation.
17th Jan 2017, 4:15 AM
Nina
Nina - avatar
+ 5
I had no clue either for years even after 3 Java courses in college. One instructor finally started covering it while I was a teaching assistant.
17th Jan 2017, 6:00 AM
Nina
Nina - avatar
+ 2
Shit.. I had Java in my academics and i never knew it.. Guess my academy book was shit too.
17th Jan 2017, 5:46 AM
Shin Rawat
Shin Rawat - avatar
+ 1
Woh.. That would have been a shock
17th Jan 2017, 6:03 AM
Shin Rawat
Shin Rawat - avatar
+ 1
Hi, By seeing your syntax I believe you're using python. When you type %s, within double codes means you are reserving space for a value to be entered. So, what the % sign does is It will instruct the compiler that that the value coming after the mod % is to be replaced with the empty reserved space.
21st Jun 2017, 12:10 PM
abul abbas
abul abbas - avatar