In Python, the simplest and most basic output display project is “Hello World”. This world represents our entry into the world of code, and the key to this world is print.


Usage of print

Below, I will teach you how to use print.

The most common usage of print we see now.

1
2
3
4
5
print(666)
print('Hello World!')
print("Hello World")
print('''Hello World,
This is the first Python program''')

The differences between these code snippets, apart from the different content being printed, are the number of quotes used. I will explain each one to you.

No quotes: Recognizing machine language

In the examples above, the code without quotes is 666. The characteristic of not using quotes is that it can only recognize machine language, i.e., numbers and operations, and the input must be an integer, i.e., a number.

Code Example

1
2
print(666)
print(6+6)

The output result of the first line of code is 666

The output result of the second line of code is 12

The second line of code does not input the result, because it includes operations in the computer language, so it will directly output the result. The operations here not only include addition, but also subtraction, multiplication, division, and exponentiation, i.e., 【+】【-】【* 】【/】【 **】. If you input a string, it will immediately report an error.

Single quotes: Output of any character

The meaning of single quotes is that whatever you input, it will output whatever you input.

Code Example

1
2
3
4
5
print('666')
print('Hello World')
print('6+6')
print('Are you a pig?')
print('Jason said "I like you".')

The output content is the content inside the quotes, but the input content must be a string.

Note: The output content of the third line is 6+6, and after adding quotes, it will not perform the calculation.

If your sentence contains double quotes, it is recommended to use single quotes.

Double quotes: Used for special or conflicting situations

The usage of double quotes is the same as single quotes, mainly used for conflicting symbols.

Code Example

1
print("I'm a code editor.")

The input content must be of string type. If the content contains single-quoted characters, you can use double quotes to display them. Of course, double quotes can also be used to display any character.

Special Cases

If you just want to use single quotes within single quotes, or double quotes within double quotes, there is also no way. We can use Python’s escape characters.

1
2
print('I\ 'm a code editor.')
print("Jason said \"I like you\".")

Triple quotes: Automatic line breaks

The output of triple quotes is basically the same as single and double quotes, but it can directly perform line breaks.

Code Example

1
2
print('''Hello World,
Hello World!''')

The output of this code is directly two lines of content, the first line outputs Hello World, and the second line outputs Hello World!

Any string can be entered within the quotes.

If an integer, i.e., a number, needs to be line breaks, you can use backslash + n to line break.

For example

1
print('1\n2')

The output result is also two lines, the first line is 1, and the second line is 2

Of course, there is also not to line break after outputting

For example

1
print(内容, end = “”)

Do not line break after outputting content

String Concatenation

This part of the content will be discussed in the data type, involving the conversion of str strings

Homework

Print Mario image

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
print('''                ********
************
####....#.
#..###.....##....
###.......###### ### ### ### ###
........... #...# #...# #...# #...#
##*####### #.#.# #.#.# #.#.# #.#.#
####*******###### #.#.# #.#.# #.#.# #.#.#
...#***.****.*###.... #...# #...# #...# #...#
....**********##..... ### ### ### ###
....**** *****....
#### ####
###### ######
############################################################## ##################################
#...#......#.##...#......#.##...#......#.##------------------# #...#......#.##------------------#
###########################################------------------# ###############------------------#
#..#....#....##..#....#....##..#....#....##################### #..#....#....#####################
########################################## #----------# ############## #----------#
#.....#......##.....#......##.....#......# #----------# #.....#......# #----------#
########################################## #----------# ############## #----------#
#.#..#....#..##.#..#....#..##.#..#....#..# #----------# #.#..#....#..# #----------#
########################################## ############ ############## ############''')