The print function in Python is a function that outputs to your console window whatever you say you want to print out. At first blush, it might appear that the print function is rather useless for programming, but it is actually one of the most widely used functions in all of python. The reason for this is that it makes for a great debugging tool.
"Debugging" is the term given to the act of finding, removing, and fixing errors and mistakes within code.
If something isn't acting right, you can use the print function to print out what is happening in the program. Many times, you expect a certain variable to be one thing, but you cannot see what the program sees. If you print out the variable, you might see that what you thought was, was not.
You can follow this tutorial with the video and the embedded console, via the text and consoles, or via your own installation of Python.
Next up, strings, what are they? Strings are just "strings" of text, hence the name. Strings are a type of data. Another type of data is integers.
To build on the video, here are some examples:
print('Single Quotes') print("double quotes")
We're printing out a string. Notice that the quotes are single quotes. You can use single quotes or double quotes, but they need to be used together.
While we're talking about strings and the print function, it would be useful to discuss concatenation. Concatenation just means the combination of things. You can use the "+" or the "," to join strings together. If you use a ",", then you will have a space in between the strings you joined. If you use a "+", then the strings will be strung together with no space. You will need to add one if you wanted.
If you use the "+" to join integers and floats together, then you will perform an arithmetic operation. If you use the ",", then it will print them out separately, with a space.
print('can do this',5)
print('cannot do this:'+5)
You cannot use the "+" to join strings with ints or floats, you must use the ",".
It is also important to bring up how to put quotes within strings. You can either put double quotes inside single quotes, singles inside doubles, or use the "\" backslash. The \ character is known as an escape character, and it will "escape" the characteristic of the following character and just take on the 'visual' aspect of it.
The purpose of the "escape character" is to escape various characteristics for characters. For example, a quotation, ", in a string might wreak havoc. Take for example: x = "He said, "Hello there!" "
Yeah, that's going to be a problem. There are obviously many options to avoid this specific problem, but one of them would be to use an escape character:
x = "He said, \"Hello there!\" "
If you do a print(x), you will not see the escape characters, and you will see the quotes. Sometimes you want to show the escape character as well:
x = "An escape character is a \"
How might you solve that?
Here are some examples of quotation rules:
print('Can't do this')
print('you\'ll have success here')
print("you'll have success here too")
It is also important to bring up how to put quotes within strings. You can either put double quotes inside single quotes, singles inside doubles, or use the "\" backslash. The \ character is known as an "escape" character, and it will "escape" the characteristic of the following character and just take on the 'visual' aspect of it. Here are some examples of quotation rules:
That covers the basics of strings for now.