Being able to produce an output is one of the most fundamental and important skills any aspiring programmer needs to learn.
Why do we need print() in Python?
Printing an output is the simplest way of confirming whether a segment of code works or not. As it can send values or information directly to the console, it is a powerful debugging tool. The print() function can be used to send confirmation messages, error messages, or general information.
How do I print a string?
The print() function is very simple and easy to use. Typically, all that this function needs is a value to output to the console. Strings are multi-character variables that represent text and is declared using double quotes. You can use print(String) to print out a line of text.
Consider the following segments of code:
Example 1:
print(“Welcome to IC 123!”)
Output:
Welcome to IC 123!
As you can see in the output, only the words inside the double quotes were printed. This is because python ignores the double quotes, as they are used to declare the string that we are printing. Python knows that the programmer does not intend for the double quotes to also be printed, and thus only the sentence we want is printed.
Example 2:
print(“USA”) print(“Canada”) print(“Germany”) print(“France”) print(”Japan”)
Output:
USA Canada Germany France Japan
The previous segment of code printed five values because we used the print() function five times.
Comments
In all programming languages, comments are messages that are ignored by the machine and is only for human use only.
Comments help explain code or are used as indicators for what each part of a program does. It is common practice and good etiquette to add comments in your code if the code will be or is meant to be read by others. Comments can also guide the users in writing their own code by avoiding ambiguity and confusion.
Comments are initiated by using the “#” symbol, and many IDEs allow you to comment on multiple lines at once using the shortcut Ctrl + Slash (or, Cmd + Slash on OSX).
Example:
#This will print “Hello World!”, and this line does nothing. print(“Hello World!”)
Output:
Hello World!
Further Reading
Single and Double Quotes
In Python, single quotes and double quotes are both used when declaring strings. The main difference is that while using single quotes for declarations, you’re able to write double quotes within your text and vice versa.
Example 1:
print(‘Welcome to “IC 123”’)
Output:
Welcome to “IC 123”
In this case, because we declared the string with single quotes, we were able to put double quotes around the “IC 123”.
Example 2:
print(“’Welcome to IC 123’”)
Output:
‘Welcome to IC 123’
Likewise, in this case because we declared the string with double quotes, we were able to put single quotes around the entire text.
Escape Sequences
Within Python, there are limitations that programmers face while printing strings. For example, what if a programmer wanted to print out double quotes? Or, what if they wanted to go to the next line?
Due to these limitations, Python has escape sequences. Escape sequences allow programmers to bypass these restrictions by using a special sequence of characters to represent the value they are trying to output.
Here is a table of useful escape sequences:
Escape Sequence | Meaning |
\’ | Single quote |
\” | Double quote |
\\ | Backslash |
\n | Newline |
Refer to the segments of code below for the application of escape sequences.
Example 1:
print(‘It\’s fun to learn Python!’) print(“We think it is very \”hard\””)
Output:
It’s fun to learn Python! We think it is very “hard”
The single and double quote escape sequences are used to insert them into the strings when we otherwise shouldn’t be able to.
Example 2:
print(“IC 123 teaches \n Python very well”)
Output:
IC 123 teaches Python very well
In this case, the newline escape sequence is used to move “Python very well” into a second line.
Example 3:
print(“This is a backslash: \\”)
Output:
This is a backslash: \
Because the backslash is used to declare escape sequences, it needs its own escape sequence to print out.
End Command
For both the end command, it is exclusive to Python 3 and beyond. The end string is passed as a parameter and is concatenated to the end of the output. You can use print(String, end=”Other String”) to add an end to your output.
Example:
print(“Python”, end=”is fun!”)
Output:
Python is fun!