Can i use %s in print whatever the type of my variable is? | Sololearn: Learn to code for FREE!
Neuer Kurs! Jeder Programmierer sollte generative KI lernen!
Kostenlose Lektion ausprobieren
0

Can i use %s in print whatever the type of my variable is?

I use %s in print() whatever the variable type is and i don't have a problem. But is it really right? Is there a memory problem with it? Is there any case i can't/shouldn't use %s in python 3? thank you

13th Nov 2017, 5:18 PM
spykate
spykate - avatar
3 Antworten
+ 1
You use %s when the variable is a string. If it is %f, you use a float, %i or %d for an int, etc. Here is a reference https://www.learnpython.org/en/String_Formatting
14th Nov 2017, 6:56 AM
Cailyn Baksh
0
so you say that even though %s works even with integer and float type variables, i shouldn't use it like that?
14th Nov 2017, 11:15 AM
spykate
spykate - avatar
0
When the object is not a string, it uses its string representation, which is returned by repr(). If you want to use the integer representation, you would use %d. For a float representation, %f.
14th Nov 2017, 3:39 PM
Cailyn Baksh