Python

Text output

Text output is one of the basics in Python programming. Not all Programs have graphical user interfaces, text screens often suffice.

You can output to the terminal with print function. This function displays text on your screen, it won’t print.

Related Courses: Complete Python Bootcamp: Go from zero to hero in Python

The terminal is a very simple interface for Python programs. While not as shiny as a GUI or web app, it’s good enough to cover the basics in.

Create a new program (text file) in your IDE or code editor.
Name the file hello.py. It only needs one line of code.

To output text to the screen you will need this line::

print("Hello World")

Run the program (from terminal: python hello.py)
If you run the program:

Hello World

The program above prints everything on a single line. At some point you’ll want to write multiple lines.

To write multiple lines, add the ‘\n’ character:

print("Hello World\nThis is a message")

Results in:

Hello World
This is a message
Note: the characters \n create a new line

To print variables:

x = 3
print(x)

This will show:

3

To print multiple variables on one line:

x = 2
y = 3
print("x = {}, y = {}".format(x,y))

Will give you:

x = 2, y = 3
Previous Post Next Post

Cookie policy | Privacy policy | ©