Data Output: print() function

Theory

The function print can output not only the values ​​of variables, but also the values ​​of any expressions. For example, the entry print(2 + 2 ** 2).

Also, using the function, print you can display the value of not one, but several expressions; to do this, you need to list them separated by commas:

a = 1
b = 2
print(a, '+', b, '=', a + b)


In this case, the text will be printed 1 + 2 = 3: first, the name of the variable is displayed a, then a line from the “+” sign, then the value of the variable b, then a line from the “=” sign, and finally, the value of the amount a + b.

Please note that the output values ​​are separated by one space. But this behaviour can be changed: you can separate the output values ​​with two spaces, any other character, any other line, display them on separate lines, or not separate them at all. To do this, you need to print pass a special named parameter to the function, called sep, equal to the string used as a separator (sep is an abbreviation for the word separator). By default, the parameter sep is a single-space string and a space is displayed between the values. To use, for example, the colon character as a separator, you need to pass a parameter sepequal to the string ':':

print(a, b, c, sep = ':')

Similarly, in order to completely remove the separator from the output, you need to pass the parameter sepequal to the empty string:

print(a, '+', b, '=', a + b, sep = '')

In order for values ​​to be output on a new line, you need to sep pass as a parameter a string consisting of a special newline character, which is specified like this:

print(a, b, sep = '\n')

The backslash character in text strings is an indication of the designation of a special character, depending on what character is written after it. The most commonly used character is the newline character '\n'. And in order to insert the backslash character itself into a string, you need to repeat it twice: '\\'.

The second useful named parameter to a function print is the parameter end, which specifies what is printed after all the values ​​listed in the function are printed print. By default, the parameter end is equal to '\n', which means that the next output will occur on a new line. This parameter can also be corrected, for example, in order to remove all additional characters output, you can call the function print like this:

print(a, b, c, sep = '', end = '')


Problems

Subscribe to receive updates!

Be up-to-date with our recent updates, new problems and answers!

Thank you! Your submission has been received!
Oops! Something went wrong while submitting the form.

Our courses

Where do you hold your classes?
We hold our classes online or on-site on Saturdays at our branch in Pimlico Academy, London.
You can find our timetable here.
What do you need to start learning online?
For lessons you only need a computer or phone with a microphone, camera and Internet access. Wherever you are - in London, Nottingham, New York or Bali - online lessons will be at hand.
When can I take the introductory lesson?
You can get acquainted with the school at any time convenient for you. To do this, just leave a request and sign up for a lesson.
I can't attend class, what should I do?
It is OK, it happens! Students have the opportunity to cancel a lesson up to 8 hours before the scheduled time without loss of payment. So you can reschedule it for a convenient time, and the teacher will have the opportunity to
I don't have much free time, will I have time to study?
Learning can take place at your own pace. We will select a convenient schedule and at any time we will help you change the schedule, take a break or adjust the program.
How long is one lesson?
All classes last 1 hour.
Hear from some of our amazing students who already achieved incredible results with us!
"Olympiad Maths Lessons helped me a lot to get the Gold medal in Junior Maths Challenge"
St. Paul's Student
"Thanks to the 'Data Science' and 'Coding in Python' lessons I got accepted to my dream university."
Michael
Data Science Student
Warwick University
"Great courses, which thoroughly explained topics beyond the capability of the GCSE answer sheet. Thanks so much."
Ivan
GCSE Student in Dubai
"Financial Mathematics! Best course to understand Python and Mathematics behind Finance!"
Gleb
VC Investor
"We got silver in PMC! Thanks George!"
Mum of St. Paul's Student
Prepare for the Primary Maths Challenge
"My daughter took a batch of 10 classes with George to understand Python with Turtle. I found George extremely knowledgeable and accessible."
Dad of Latymer School Student
Python with Turtle
We’re hiring!

Meet our team

Our teachers will tell you how to prepare for exams, help you cope with difficult tasks and win the Olympiad

They will tell you about the pitfalls of exams and the most common mistakes, and explain how to avoid them
George Ionitsa
Founder &
Teacher of Maths and Coding