Introduction
The print
function in Python is a built-in function that allows you to display messages or the values of variables to the console. Itβs one of the most basic and essential functions youβll use in Python programming.
The basic syntax for the print function is:
print("Hello World!")
This will print the string βHello World!β to the console. You can also concatenate multiple strings using the β+β operator. Letβs see an example:
print("Hello" + " " + "World!")
It will also print the same message as the above: βHello World!β.
Printing Variables
Letβs see how you can print values from variables using the print function:
name = "Subhankar" age = 25 print("My name is", name, "and I am", age, "years old.")
The above code will print the string βMy name is Subhankar and I am 25 years old.β Notice that we didnβt use string concatenation in this case, but instead used commas to separate the variables.
Printing Expressions
You can also print expressions using the print function. For example:
x = 3 y = 5 print("The sum of", x, "and", y, "is", x + y)
This will print the string βThe sum of 3 and 5 is 8β. Notice that we used the β+β operator to calculate the sum of the variables inside the print function and included it in the string.
Printing with Formatting
Another way to print variables and expressions is by using string formatting. Python supports several methods for string formatting, including f-strings, the β%β operator, and the βformat()β method.
Learn more about Strings in Python here.
f-strings
name = "Subhankar" age = 25 print(f"My name is {name} and I am {age} years old.")
This code will print the same string as the previous section, but using an f-string. The values within the curly braces are evaluated and incorporated into the string.
Using the β%β operator
name = "Subhankar" age = 25 print("My name is %s and I am %d years old." % (name, age))
This code will also print the same string as before, but using the β%β operator. The β%sβ placeholder is used for strings, and β%dβ is used for integers.
βformat()β method
name = "Subhankar" age = 25 print("My name is {} and I am {} years old.".format(name, age))
Again, this code will also print the same string as before, but using the βformat()β method. The placeholders in curly braces are replaced with the variables passed as arguments to the βformat()β method.
Escape Sequence
In Python, you can use an escape character or backslash(β\β) to avoid the illegal characters in a string. Letβs see an example:
print('It's My first Python Program')
The above code will raise syntax errors due to the presence of a single quotation (β) mark within the paragraph text.
The simple way to solve the above issue is to add an escape character (β\β) before any unauthorized characters, as demonstrated in the following example:
print('It\'s My first Python Program')
Hereβs another example of printing illegal strings. Note that βhostsβ is an unacceptable format within the string. However, by adding the β\β character beforehand, this issue can be resolved.
print("Every Operating System has a \"hosts\" file.")
Printing Raw Strings
Raw strings are special strings in programming that ignore backslashes (β\β). This is useful when you want to include backslashes in your string without having them interpreted as escape sequences.
For example, the following string would print a newline character:
text = "This is a string with a newline \n" print(text)
This can be avoided using a raw string:
text = r"This is a string with a backslash \n" print(text)
This would print: βThis is a string with a backslash \nβ.
Printing New Lines
When we write a program, it is often necessary to print multiple new lines. In Python, printing new lines is accomplished by using the β\nβ character. Take an example:
print("1. First Line\n2. Second Line\n3. Third Line")
The output would be:
1. First Line
2. Second Line
3. Third Line
Exercises
Try some exercises for practicing the Python βprint()β function:
- Print the string βHello, World!β to the console.
- Print a string that includes double quotes, single quotes, and a backslash.
- Print the result of a mathematical expression, such as 2 + 2 or 5 * 7.
- Print a multi-line string using triple quotes.
- Print a formatted string that includes variables, such as βMy name is [name] and I am [age] years old.β