-4
my_variable_name = str("John")
print(my_variable_name)

This is the code for example, now if I happen to add the double quotes around "my_variable_name" in this statement:

print(my_variable_name)

It just simply display whatever is written inside the "" but if I don't add it'll print "john". Now what I think the reason is because when compiler find something inside "" that tells it to display whatever datatype it is while without "" it just displays the stored or u can say assigned value . I know its easy (basis) but I do this like all the time and my knowledge about this problem never satisfies me

OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
  • Please clarify the question. As currently stated, it’s unclear what you’re expecting from the `print` function. (`printf` tag removed as this has nothing to do with `printf`.) – S3DEV Feb 13 '22 at 16:17
  • What do you have problem understanding - the concept of variable/python names, the concept of str literal (i.e. something enclosed in single quotes, double quotes, triple single or tripple double quotes)? – buran Feb 13 '22 at 16:21
  • You should familiarize yourself with the concept of variables and strings (and other types, for that matter); it's essential to programming. However, Stack Overflow is not the place to learn that. Please read [ask] and the [help] for more information on asking good, [on-topic](https://stackoverflow.com/help/on-topic) questions. – Sylvester Kruin Feb 13 '22 at 16:22

1 Answers1

1

The problem has nothing to do with print function.

"my_variable_name" is a string literal. Single or triple quotes could also be used.

my_variable_name is a reference to a previously defined variable of that name. The type of the variable's value could be anything.

You can print any object, and it'll return the str() representation of it.

Unrelated, you don't need str() function to define a string literal.

when compiler find something inside "" that tells it to display whatever datatype it is

Python is an interpreted language, it's not the compiler doing this. The datatype of anything enclosed in quotes is always a string

OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
  • 1
    How did you answer a closed question? Is it a privilege that high-rep users have? – Sylvester Kruin Feb 13 '22 at 16:24
  • It's not. I was able to submit before it closed – OneCricketeer Feb 13 '22 at 16:33
  • That's odd. Looking at the specific times, it says the question was closed at `2022-02-13 16:21:21Z`, while your answer was posted at `2022-02-13 16:23:00Z`. Also, your answer says "answered 13 minutes ago", whereas the close bar at the top of the question says "closed 15 mins ago". Does the system really take 1.5 minutes to process an answer and post it? – Sylvester Kruin Feb 13 '22 at 16:37
  • Dunno. Sometimes question get closed while I type answers and it'll gray out the submit button. Other times, not. – OneCricketeer Feb 13 '22 at 16:42
  • ok thanks for your answer , but as u can see I am a newbie , how did my question got closed , can users like you do it or its automated – Gaurav Kumar Feb 13 '22 at 17:24
  • @Guarav Three people voted to close the post because they didn't understand what you're trying to ask, or they believe the answers to your question could be found following more official Python documentation – OneCricketeer Feb 13 '22 at 17:27
  • Ohh! was it that irrelevant :( – Gaurav Kumar Feb 13 '22 at 17:31
  • @Guarav The question itself isn't really Python specific. Most programming languages have clear differences between variable references and string literals wrapped in quotes – OneCricketeer Feb 13 '22 at 17:37
  • Well, I guess [this](https://meta.stackoverflow.com/a/252714/16775594) answers my question about answering-when-closed. It's a grace period for people who have already started writing answers. I just saw another instance of this, so I searched meta for why it happens. – Sylvester Kruin Feb 15 '22 at 20:15