Print function in Python

Photo by C M on Unsplash

Print function in Python

The function of the print() statement in Python is indeed to display specified messages or values on the screen. The values can be strings, numbers, or any other data type. When using print(), the parameters are automatically converted to strings before being displayed

One can convert the required parameter to strings by using

  • The str() function: - The str() function in Python is a built-in function that is used to convert any specified value to its string representation. It takes a single argument, which can be of any data type (e.g., integer, float, boolean, list, tuple, etc.), and returns the corresponding string representation of that value.

  • Concatenation: - Concatenation is an operation in Python that involves combining two or more strings together. It is performed using the + operator. When you use the + operator with two strings, it joins them end-to-end to create a new string.

Syntax of the print function

print()

Let us take some examples to understand how the print function works

String literals in Python

It is used to design or format the appearance of the string when it is printed by using the print function. Now let's have a look at how string literals get formatted in the print function and understand their function of it: -

  • \n: - The \n is called the "newline escape sequence" in Python. It is used to add a new line (line break) in the output when using the print() function. When \n is included in a string, it acts as a special character that represents the end of the current line and starts a new line in the output.

  • " ": - This is not considered a function or a literal in Python. Rather, it is an empty string enclosed within double quotes. An empty string is a string that contains no characters. It is used when you want to print an empty line or have a line break without any content.

Example

print("hello \n how are you.")

End parameter in Python

In Python, the end parameter is an optional argument that can be used with the print() function to specify what should be printed at the end of the output. By default, the end the parameter is set to '\n', which means that a new line character is added at the end of the printed output, causing the next print statement to start on a new line.

However, by providing a different value to the end parameter, you can change the behavior of the print() function and control what character (or string) appears at the end of the output.

Here's the general syntax of the print() function with the end parameter:

Example

print(*objects, sep=' ', end='\n', file=sys.stdout, flush=False)

Error Scenario

Certainly! To avoid the scenario where the quotation marks are not closed and the closing parenthesis is missing, always ensure that you close the quotation marks before providing the closing parenthesis for the print() function.

Note: - Always pay attention to properly closing the quotation marks and parentheses to avoid syntax errors in Python code.

Code Example: -

print("hello)

Output

File "main.py", line 1
    print("hello)
          ^
SyntaxError: unterminated string literal (detected at line 1)