Python Programming - Basic Input and Output - print and input functions
Appficial Appficial
8.47K subscribers
21,621 views
0

 Published On Aug 12, 2021

To print output to the screen, use the print() function. Ex: print('hello world’).

The text ‘hello world’ is called a string literal. A string literal may contain any characters such as letters, numbers or symbols such as $ or #. In python, you can use either single or double quotes to surround a string literal. print(‘hello world’) and print(“hello world”) both print the text hello world to the screen. If text represents numbers such as 187, or 11.99, then there are no quotes around the numbers. A string ‘187’ is different than a number 187 in Python. 187 represents the number 187, while string ‘187’ is a character sequence ‘1’, ‘8’ and ‘7’.

If you need to print out a combination of string literals and variables, separate each item with a comma. Ex:

age = 20
print(‘Your are’, age, ‘years old.’) # Comma separates multiple items with a space
print(‘Have a nice day!’)

Outputs:

You are 20 years old.
Have a nice day!

Calling the print() function outputs on a new line. If you want to keep output on the same line., then add end=' ' inside of print() to separate the next line by a single space. Ex: print('Hello', end=' ‘).

You can print out the newline character ‘\n’ whenever you need to output a new line. Ex: print(‘A\nB\nC’) outputs:

A
B
C

A whitespace refers to any space, tab, or newline in your program. Whitespace matters in Python, as you will see soon as you learn more python.

Programs may allow a user to enter input values, such as a number or string. In python, you can read input using the input() function. Ex: name = input() will set the variable name to the text entered by the user.

Any data read by the input() function is read in as a string, so if you wanted to convert string ‘187’ to the interger 187, then use the int() function. Ex: some_int = int(‘187’)

Subscribe to APPFICIAL for more programming videos coming soon. Also be sure to click LIKE and comment on the video!

show more

Share/Embed