Some basic concepts

Python strings can be represented in one of three ways

“”” and “” (or “””)

The first two are single-line strings, single quotes are unescaped, double quotes are escaped, and the latter is a multi-line string

In addition, you can also use \ to escape characters (such as \n).

Triple quotes are multi-line strings. If you do not want to use triple quotes to implement multi-line strings, you can do so by adding \ to the end

print("1st \n\
2nd")
Copy the code

And multi-line comments in Python are also implemented in triple quotes

# This is a one-line comment
This is a multi-line comment.
Copy the code

Do not escape strings

Use the single quoted string directly or prefix the string with r

>>> print('\\\t\\') \ \ > > >print(r'\\\t\\')
\\\t\\
Copy the code

Formatted string

The traditional way

>>> name = 'Runoob'
>>> 'Hello %s' % name
'Hello Runoob'
Copy the code

f-string

>>> name = 'Runoob'
>>> f'Hello {name}'  # replace variable
'Hello Runoob'
>>> f'{1 + 2}'         # use expressions
'3'
>>> w = {'name': 'Runoob'.'url': 'www.runoob.com'}
>>> f'{w["name"]}: {w["url"]}'
'Runoob: www.runoob.com'
Copy the code