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:
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:
- 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):
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:
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”)
>>> print(“Hello world”,file=open(‘hello.txt’,’a’))
>>> print(“Hello”,”world”, sep=’\n’)
>>> for i in range(3):
… print(“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(“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 escape character, 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.