python

Output using print() function in Python

To get output in Python, there is the function print(). A character string can be entered within the brackets, which is then displayed on the screen. To do this, this string is put in quotation marks. It doesn’t matter whether you have double or single quotation marks!

print('Hello World!');

The quotation marks in brackets are used to enclose the output and are not displayed.

With the function print() we can also output the content of variables. For this purpose, the variable name is simply written in the brackets (in any case without quotation marks, otherwise the variable name is simply output as a character string and not the content of the string).

print(myvar)

If the variable has not yet been assigned a value, an error message (traceback) is displayed: NameError: name ‘myvar’ is not defined

So, first, define the value of the variable before output using print():

name = 'Jean'
print(name)

 

mcq-pythonPython MCQ and Answers – ListsThis collection of Python Multiple Choice Questions and Answers (MCQs): Quizzes & Practice Tests with Answer focuses on “Lists”.   1. Which of the following…Read More
Output blank lines via print()

We can also output a blank line by using an empty print():

print("Hello World!")
print()
print("Welcome to StackHowTo")

There are other ways to output a blank line.
 

Special case backslash

If a variable contains a backslash, then what the output makes of it is exciting. Of course, you may wonder when you should have a backslash in a variable. Suppose we want to store a pathname from disk in a variable. Then the notation would be:

path = 'C:\namedoc\images'
path = "C:\namedoc\images"

Let us now output our variable path via print().

print(path)

Now we get an output wrapped over 2 lines without the backslash:

C:
amedoc\images

What happened. We already got to know the backslash with the variables. Here it was there to mask certain characters. So our backslash has more than one meaning. If we want to output a backslash, we have to mask the backslash with a second backslash:

path = "C:\\namedoc\\images"

But what really happened in the background? There are so-called control sequences that affect the output. And we accidentally caught such a control sequence with \n.

\n stands for the line break.

So if we want to specifically output a blank line, then simply use the line break \n \n twice in print().

Control sequences for tabulator:
Other control sequences are e.g.
\t for the tab. Just test it once in print().
 

 

Output with print(r)

If you want to stop the print() function from interpreting this value, you can force a “raw” output with the “r” at the beginning. Then you also get the pure output:

print(r"C:\namedoc\images")

However, this is not really practical with variables:

print(rpath)

Now we have an error message!
 

Multiple breaks in the output

Breaking the output with the control character is one way of creating multiple lines in the output. The whole thing worked by adding 3 quotation marks at the beginning and of course at the end. Both double and single quotes can be used here. Only the same should be used:

print("""Hello
World
 – in 3 lines""")

Output:

Hello
World
 – in 3 lines
mcqMCQPractice competitive and technical Multiple Choice Questions and Answers (MCQs) with simple and logical explanations to prepare for tests and interviews.Read More

Leave a Reply

Your email address will not be published. Required fields are marked *