What you type into your machine is input. What the machine produces for you is output. Let’s have a closer look at these two, starting in this lesson with the latter, so text output, and continuing with the former in the next lesson.
Table of Contents
Output
One of the most common tasks in programming is printing texts: just a word, a warning, a message, some information, etc. In Python text is represented by a special data type called string. So, a string is a series of characters – it may be just one character, a word, a sentence or a whole lengthy text.
Strings are typed between single or double quotes. In interactive mode we can echo strings:
>>> 'hello'
'hello'
>>> "hello"
'hello'
We can assign strings to variables and echo the variables:
>>> text = "hello"
>>> text
'hello'
Standard Output
We can send a string to the standard output using the print() function.
If we use the print() function, we get the output without quotes.
The print() function takes a parameter, which is the string to be printed. We can use literal text in quotes:
>>> print('hello')
hello
We can also pass a variable to the print() function. The name of the variable is without quotes:
>>> text = "hello"
>>> print(text)
hello
When using the print() function, a newline is appended to the string. Let’s write a short program in a file and run it. Here, after the string is printed, a newline takes us to the next line:
print("hello")
and here’s the output:
hello
Press any key to continue . . .
If we don’t like this behavior, we can use the end parameter with the string we want to be at the end of the string when printed:
print("hello", end = "!!!")
Now the output is:
hello!!!Press any key to continue . . .
Or we can add some characters plus a newline using the newline escape character \n:
print("hello ", end = "!!!\n")
Now the output is:
hello!!!
Press any key to continue . . .
If we want nothing at the end, we can just append an empty string:
print("This is a text.", end = "")
Now we’re still in the same line:
This is a text.Press any key to continue . . .
Strings may be concatenated by means of the + operator:
>>> "This is" + " my house" + "."
'This is my house.'
>>> text1 = "This is"
>>> text2 = " my house"
>>> text3 = "."
>>> print(text1 + text2 + text3)
This is my house.
And there’s even more to the print() function than that. We can pass any number of arguments to the function and it will print them all in the same order, separating them with blanks:
>>> a = 5
>>> b = "hello"
>>> c = "hi"
>>> print(a, b, c)
5 hello hi
>>> print(b, c, a)
hello hi 5
Here’s an example with even more arguments. What’s interesting, the function also evaluates arithmetic expressions like the addition in the example below:
>>> print ("hello", 37, 2 + 3, "no", 7)
hello 37 5 no 7
If we want the strings in the output to be separated by something else, not blanks, we can define our own separator by setting the value of the keyword parameter sep to a string consisting of one or more characters:
>>> print ("hello", 37, 2 + 3, "no", 7, sep = "*")
hello*37*5*no*7
>>> print ("hello", 37, 2 + 3, "no", 7, sep = "#####")
hello#####37#####5#####no#####7
>>> print ("hello", 37, 2 + 3, "no", 7, sep = " as well as ")
hello as well as 37 as well as 5 as well as no as well as 7
If we set sep to an empty string, the strings in the output will not be separated at all:
>>> print ("hello", 37, 2 + 3, "no", 7, sep = "")
hello375no7
QUIZ
1. We can send a string to the standard output using the … function. |
A) output() |
B) print() |
C) write() |
2. Strings may be concatenated by means of the … operator: |
A) + |
B) * |
C) # |
3. If we pass multiple arguments to the print() function, it will print them all separated with: |
A) commas |
B) blanks |
C) newline characters |
TRUE OR FALSE?
1) Strings are typed between single or double quotes. |
2) When using the print() function, a newline is appended to the string. |
WHAT’S THE OUTPUT?
1) >>> text = “OK” >>> text |
2) >>> “What” + ” are ” + “you” + ” ” + “doing?” |
3) >>> a = “This is ” >>> b = “a car. ” >>> print(a + b + “A good one!”) |
SOLUTION
QUIZ
1. We can send a string to the standard output using the … function. |
A) output() |
B) print() |
C) write() |
2. Strings may be concatenated by means of the … operator: |
A) + |
B) * |
C) # |
3. If we pass multiple arguments to the print() function, it will print them all separated with: |
A) commas |
B) blanks |
C) newline characters |
TRUE OR FALSE?
1) Strings are typed between single or double quotes. True |
2) When using the print() function, a newline is appended to the string. True |
WHAT’S THE OUTPUT?
1) >>> text = “OK” >>> text Output: ‘OK’ |
2) >>> “What” + ” are ” + “you” + ” ” + “doing?” Output: ‘What are you doing?’ |
3) >>> a = “This is ” >>> b = “a car. ” >>> print(a + b + “A good one!”) Output: This is a car. A good one! |
PROJECT
'''
Text Output - Project
TO DO LIST
__________________________________________________________
Your task is to create a list of things to do that looks like this:
I'm very busy. Here's what I have to do:
watch TV, then sleep, then eat dinner, then sleep, then take a rest
And here's how I'm doing:
watch TV - done
sleep - done
eat dinner - just doing
sleep - still to do
take a rest - still to do
Follow the steps below to complete the task:
#1. Create variables that store the values of all the activities.
Name them: # activity1, activity2, etc.
The activities are: watch TV, sleep, eat dinner and take a rest.
#2. Create a variable named intro that contains a string obtained
by concatenating the following two strings:
"I'm very busy. "
"Here's what I have to do:"
#3. Create a variable named progress_info that is obtained by
concatenating the following string:
"And here's how I'm doing:"
and a newline character.
#4. Create three variables that hold the status: done, just doing
or still to do. Name them status_done, status_doing and status_todo
respectively.
#5. Write code that will print the intro text stored in the intro variable.
#6. Write code that will print the following line:
watch TV, then sleep, then eat dinner, then sleep, then take a rest
Use the variables activity1-4 and a separator.
#7. Write code that prints an empty line. To do that, you just have to
use the print function with empty parentheses.
#8. Write code that prints the value of the proress_info variable.
#9. Write code that prints the status of each activity, like so:
watch TV - done
sleep - done
eat dinner - just doing
sleep - still to do
take a rest - still to do
Use the variables defined before for each activity and for each status.
To separate the activity from the status use a separator that consists
of a dash surrounded by white spaces.
The separator should be first saved in a variable called dash and you
should use this variable in the print function instead of typing the
separator string directly.
'''
PROJECT SOLUTION
'''
##1.
activity1 = "watch TV"
activity2 = "sleep"
activity3 = "eat dinner"
activity4 = "take a rest"
##2.
intro = "I'm very busy. " + "Here's what I have to do:"
##3.
progress_info = "And here's how I'm doing:" + "\n"
##4.
status_done = "done"
status_doing = "just doing"
status_todo = "still to do"
##5.
print(intro)
##6.
print(activity1, activity2, activity3, activity2, activity4, sep = ", then ")
##7.
print()
##8.
print(progress_info)
##9.
dash = " - "
print(activity1, status_done, sep = dash)
print(activity2, status_done, sep = dash)
print(activity3, status_doing, sep = dash)
print(activity2, status_todo, sep = dash)
print(activity4, status_todo, sep = dash)