The print function in Python and the difference between single quotes and double quotes

One, the print function in Python

Usage of print function in Python:

print(“hello world!”)
hello world!

print(5+8)
13

print("I love" + "you!")
I loveyou!
Note that in Python, there is no English semicolon after the print function.

Two, single quotes, double quotes

print(‘Hello world!’)
Hello world!

print("Hello world!")
Hello world!
It can be seen that there is no difference between double quotes and single quotes when outputting strings. When there are single or double quotation marks in the string, direct printing may cause errors

1. For example: Let's go!

method one:

print("Let's go!")
Let's go!
Method 2: Use the escape character backslash "\"

print('Let's go!')
Let's go!
2, if there are single or double quotes in the string

print("I love'Apple'") #When there are single quotes in the string, just use double quotes

I love ‘Apple’

print('I love “Aplle”') #同理

I love "Aplle"
3. If you want to print a string with many backslashes:

print("C:\now") #\n is treated as a newline
C:
ow

print("C:\now") #Add a backslash to escape
\ C:\now

print(r"C:\new\near\now\no") #When the string is very long, you can use the original string, just add r
C:\new\near\now\no
#>> > print(r"C:\new\near\now\no")
#When the original string has a backslash at the end, an error will occur #SyntaxError: EOL while scanning string literal

print(r"C:\new\near\now\no"+"\") #Solution, splicing a backslash
C:\new\near\now\noSupplement
: triple quotation marks

Use triple quotes to output multiple lines of text

print("""
1!
22!
333!
4444!""")

1!
22!
333!
4444!

Guess you like

Origin blog.csdn.net/weixin_48347480/article/details/112981970