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