Printing values in Python (1/100 Days of Python)

Martin Mirakyan
4 min readJan 2, 2023

--

100 Days of Python blog post series telling that the day 1 covers the print() function
Day 1 of the “100 Days of Python” blog post series covering the print() function

When writing programs, we usually send commands to the computer and then see the results of those commands displayed in some form. One of the ways to see messages from the program in Python is by using the print function. The print function is a built-in function in Python that allows you to display output. It is a simple and commonly used function in Python, and it is used in the following way:

print('Hello, World!')

This will output the text Hello, World! to the console.

Note that after the print there are opening and closing brackets which tell the program what exactly we want it to print in the output.

When working with textual values, we put those in quotes ' to tell Python that it’s a piece of text that we would like to work with. In this case, we wrote the text Hello, World! inside the quotes to make sure Python treats it as text and prints Hello, World! in the output. If we forget to put the textual values inside quotes (e.g. print(Hello, World!)), Python would complain resulting in an error, and the program won’t be executed.

You can also use the print function to print multiple values by separating them with a comma:

print("Hello,", "World!")
print('I', 'really', 'love', 'Python')

This will output the string Hello, World! to the console, with a space between the two strings on the first line and I really love Python on the second line.

Note that there is no difference between putting a single quotation mark ' and a double quotation mark ". Python treats both exactly the same. It’s totally valid to put the textual values in both single quotation marks print('hello') and double ones print("hello"). We can even have different values in different quotation marks print("hello", 'world'). Yet, it’s recommended to pick one style and follow that for consistency. In this series, we’ll mostly use single quotation marks.

Using Python as a simple calculator

We can use Python as a simple calculator to perform some numeric operations and display the results in the output:

print(2 + 3)
print(5 - 2)
print(4 * 7)
print(9 / 3)
print(2 ** 3)
print(8 + 4 / 2)
print((8 + 4) / 2)

This code will print 5 3 28 3.0 8 10.0 6 each on a separate line. Note that Python respects the math calculation order — it first calculates whatever is written inside parentheses, then exponents, then multiplication and division, and only after that additions and subtractions.

So, we can use Python as a simple calculator to perform some arithmetic operations and see the results.

Advanced Usage (sep)

So far, we’ve seen different examples of using the print function. When printing several values, we can separate them using commas and Python will print those side-by-side separated by a space. We can also print several values with different print statements, which will print those values on separate lines. What if we would like to separate those values with something else other than the space? What if we don’t want to move to a new line after printing?

There are a few optional arguments that you can use with the print function. One of these is the sep argument, which allows you to specify a separator to use between the values being printed. For example:

print('Hello', 'World!', sep='|')
print(42, 'is a great', 'number', sep='-')

This will output the string Hello|World! to the console, with a | character between the two strings on the first line. The send line will contain 42-is a great-number. So, instead of separating the values by a space, the print function will separate those using a hyphen (-).

Advanced Usage (end)

Another optional argument is the end argument, which allows you to specify a string to be printed after the values. By default, the end argument is set to a newline character, so the print function will start a new line after printing the values. However, you can change this by setting the end argument to a different string. For example:

print('Hello,', 'World!', end='!!!')

This will output the string Hello, World!!! to the console, with no newline character at the end. This is especially helpful when printing several things:

print('Starting a calculation', end='...')
print(4, 3, sep='*', end=' ')
print('Done:', 4 * 3, sep=' -> ')

This will print Starting a calculation...4*3 Done: -> 12 all on one line.

There are a few other optional arguments that you can use with the print function, but these are the most commonly used.

What’s next?

--

--

Martin Mirakyan
Martin Mirakyan

Written by Martin Mirakyan

Software Engineer | Machine Learning | Founder of Profound Academy (https://profound.academy)

No responses yet