python3 print function

This is a follow-up article to our earlier post, Introduction to programming using python3. In our last post, we got a brief introduction of what the python programming language is, where it can be used, installing python on your machine and writing our first python program and running it in your machine. In our earlier article, we learned how to write our first python program. In this part of the tutorial, will look at the print function, string concatenation, and escape character in python3.

In our very first python program, we had just one line:

print("Hello World")

We passed the string Hello World to the print function and it displays the output in the console. Unlike python2 where print is a ‘statement’, in python3, print is a ‘function’. You could also call the print functions with single quotes or three single quotes instead of double quotes.

print('Hello World') and

print('''Hello World''') will give the same results.

The print function can accept 4 different parameters: i.e. sep, end, file, and flush. To look at the description of the function, you can type: help(print)

  • file: a file-like object (stream); defaults to the current sys.stdout.
  • sep: string inserted between values, default a space.
  • end: string appended after the last value, default a newline.
  • flush: whether to forcibly flush the stream.

Some functions you can perform by passing custom parameters to the print function are:

  • Writing to a file instead of displaying on system standard output (by default monitor):
    print("Hello world",file=open('hello.txt','a'))
    This will open a file named ‘hello.txt’ in the working directory and add the line ‘Hello World’ to it.
  • Writing multiple lines with a single print function:
    print("Hello","world", sep='\n')
    This will add a new line between the words “Hello” and “world”.
  • Adding a ‘blank space’ at the end of the value in print function:
    for i in range(3):
        print("Hello World",end=" ")

>>> print(“Hello World”)
Hello World
>>> print(“Hello world”,file=open(‘hello.txt’,’a’))
>>> print(“Hello”,”world”, sep=’\n’)
Hello
world
>>> for i in range(3):
… print(“Hello World”)

Hello World
Hello World
Hello World
>>> for i in range(3):
… print(“Hello World”,end=” “)

Hello World Hello World Hello World >>>

There are a few concepts like writing to a file, string concatenation, and range function in the examples above that we haven’t discussed yet. But we’ll talk about those as we go along.

String concatenation and escape character:

In one of the examples above, we wrote print("Hello","world", sep='\n') The sep parameter is used to add a new line in between two words “hello” and “world” and “,” is used to cocatenate the strings. Since by default, the value of sep is ” ” (a blank space), if you write just print("Hello","world"), the words will be separated by a space. Another way to join two strings is to use the “+”. However unlike “,” if you use a “+”, then the strings will be concatenated together with no space. You will need to add one if you want a space. For eg: print("Hello "+"world")The thing to notice here is that there is a space after the word “Hello”.

In python, like most languages, “+” operator is also used for arithmetic operations. So, if you are printing variables by passing “,” or “+”, the results might be different. For eg:

a,b = 0,1
>>> print(a,b)
0 1
>>> print(a+b)
1
>>> print(“Value of a”,a)
Value of a 0
>>> print(“Value of a”+a)
Traceback (most recent call last):
File “<stdin>”, line 1, in <module>
TypeError: must be str, not int

In the example above, first, we assign values 0 and 1 to variables a and b. The command print(a,b) prints values for a and b while the command print(a+b) adds the two values and prints the results. One other thing to notice is also that while concatenating two values using the “+” operator, both of them must be a string. Or else, you will face an error similar to the one the last line. A way to do that is:

> print(“Value of a”+str(a))
Value of a0

Notice there is no space between a and 0. Do you know why? How can we fix it?

Escape Character: The \ character is known as an , and it will “escape” the characteristic of the following character and just take on the ‘visual’ aspect of it. It is useful if you want to print a single quote inside a single quote or a double quote inside a double quote. For eg:

>>> print(‘This won’t work’)
File “<stdin>”, line 1
print(‘This won’t work’)
^
SyntaxError: invalid syntax
>>> print(‘This isn\’t wrong’)
This isn’t wrong

A print statement with double quote can also print single quote without the use of escape character and similarly, a print statement with single quote can print double quote without the use of escape character. A print statement with three single quotes can print both single and double quote without the use of escape character.

> print(‘python is “Awesome”‘)
python is “Awesome”
>>> print(“I’m having fun”)
I’m having fun
>>> print(”’I’m loving “python””’)
I’m loving “python”

This is the basics of the print function in python3. Like us on facebook to get notified when the next article comes out.

About Aryal Bibek 16 Articles
Learner and practitioner of Machine Learning and Deep Learning. Ph.D. student at The University of Texas at El Paso. Admin and Founder at python36.com

1 Trackbacks & Pingbacks

  1. Introduction to programming using python3 | Python 3.6

Leave a Reply

Your email address will not be published.




This site uses Akismet to reduce spam. Learn how your comment data is processed.