Are quotes always necessary inside a print()?

Question

Within the context of this exercise which introduces the print() function, are quotes always necessary inside of the print()?

Answer

Quotes are not always necessary inside of a print() function. They are only needed when the input value of the function is a string, like "hello", which you will learn more about in exercise 4. print() functions can be used to output many other values in Python, such as:

print(10) 
print(False) 
print(["adam", "betty"]) 
19 Likes

6 posts were split to a new topic: Can a variable name contain spaces?

2 posts were split to a new topic: Can I use printf and ; in python

Why does the code print(12345) shows output, but when i do the same thing with print(jacob) it does not run through at all??

7 Likes

because 12345 is a integer, while jacob is undefined variable.

python knows what integers/numbers are, and it knows strings (piece of text), but variable need to be named first.

16 Likes

Are Python functions case sensitive ? i know most of the programming languages

Print() or print()

2 Likes

It’s a very quick to test this but yes, functions and indeed all names in Python are case sensitive.
It is best to follow the style guide when using names in Python in which case keep your functions lower case and separated by underscores-

3 Likes

what this script mean?
the result is:

[‘adam’, ‘betty’]

and why double quote become single quote in the output?
whats the brackets do?

3 Likes

The square brackets create a list. If you print the list itself it will show square brackets in the output to make it clear that the object being printed is a list. The two elements in that particular list are both strings and the standard way of displaying them with print is with '' rather than "". Printing in this way is just to try and provide something that can be read by a human being. It’s just a representation of the objects that are stored.

10 Likes

Because jacob is a string value and it has to be encompassed in double commas or single ones.
e.g. print(“jacob”) will give you the output jacob

quotes are sometimes necessary to print when they are ambiguous.

To put it simply: Python tends to “assume” the meaning of certain words or numbers unless you tell it otherwise.
Let me use an analogy:
You and a friend are in a clothing store looking for party clothes. Your friend tries on a dress, and she says to you, “This is hot!”
“I agree! You look so cute in that dress!” you reply with sincere enthusiasm.
Your friend bursts out laughing.
“That’s not what I meant!” she says. “I mean I’m sweating bullets! This won’t do at all!”

Likewise, Python automatically assumes that anything inside quotes is something called a string. A string is basically a line of text. Any text that’s not surrounded by quotes will be assumed to be something else. For example, you can print the print() function itself with this code:

print("print()")

But if you write it without quotes…

print(print())

…then you won’t see print() in the output. You’ll get something else instead because the first print() function will think that you’re just trying to use the print() function again.

In your particular example, print(12345) works how you expected it to because Python assumed that 12345 is a number (just like how you assumed). But it couldn’t do print(jacob) because Python doesn’t know what jacob is. Python thinks that you’re trying to print a variable named jacob, so it tries to find out what you put inside jacob. If you don’t actually have a variable named jacob, then it will give you an error because it won’t be able to find it. BUT if you surround jacob in quotes, then Python will immediately know that you meant jacob as text and not as a variable:

# This example will actually # print the word "jacob" print("jacob")

Python’s “assuming feature” might seem like a hurdle to you at first, but soon you’ll come to appreciate it because, when you know how to use it, it will enable you to tell the computer how to do exactly what you want it to do, even in the exact way you want it done.

Hope this helped,
Nicholas

8 Likes

you should explain each problem , first time i came across someone that explains it all the way and not like they are some guru thankyou thankyou

2 Likes

Very nicely explained. Thumbs up for you, now I shall continue on to the next lesson! :grin:

1 Like

I appreciate your contribution to the thread, I don’t think I’ve seen this concept explained better online. I’m looking forward to getting your textbook - if you have written one lol. If not, I believe anyone who comes across your work, would support a Kickstarter, or similar, to help fund a pursuit. :pray: :clap:

1 Like

What are some situations where using quotes in print statements might not be necessary, and how do you know when to use them and when to leave them out?

You have to use quotes in print statement when you want to convey a text type output, or a string type. Otherwise, you can print a function, or a variable, and in this case you not use a quotes in the print statement.

See a example:

# Using a variable my_name = "Guilherme" print(my_name)

So, you always use quotes in print statement when you must to print a text type (we called it of a string) or any data that has a type of text. In others case, you can use directly, without quote, a variable name, a function name, and so on.

1 Like

I just wanted to say a big thank you for your response to my Python question! Your answer was so clear and helpful, and it really made a difference for me as I’m learning to code.

Your explanation of using quotes in a print statement was perfect - I loved how you provided an example to show exactly when quotes should be used. It made a potentially confusing concept much easier to understand.

As someone who is still new to coding, I really appreciate when experienced coders like you take the time to help out us beginners. Your kindness and expertise make the coding community such a wonderful and supportive place to learn and grow.

Thank you again for your help and guidance. You’re truly making a difference for people like me who are just starting out on this exciting journey!

1 Like

@bit8430783810 No. Quotes are there to denote that the object being printed is a string.