Detailed explanation of Python strings (including long strings and original strings)

c2b8f16e631949dfb4b550d2ba97e4ba.png

 

A collection of several characters is a string.  Strings in Python" " must be surrounded by double quotes or single quotes ' '. The specific format is:

"String content"
'String content'

The content of the string can include letters, punctuation, special symbols, Chinese, Japanese and other texts in the world.

The following are all legal strings:

  • "123789"
  • "123abc"
  • "http://c.biancheng.net/python/"
  • "C Language Chinese Network has been established for 8 years"


There is no difference between double quotes and single quotes in Python strings. In some programming languages, double-quoted strings can parse variables, but single-quoted strings are always output unchanged, such as  PHP  and  JavaScript .

Handling quotes in strings

When quotation marks appear in the string content, we need to perform special processing, otherwise Python will cause parsing errors, for example:

'I'm a great coder!'

Since the above string contains single quotes, Python will pair the single quotes in the string with the first single quote, which will treat it 'I'as a string, and the following m a great coder!'will become redundant content, resulting in Grammatical errors.

For this situation, we have two solutions:

1) Escape the quotes

\You can escape the quote by adding a backslash before it , causing Python to treat it as normal text. For example:

 
  1. str1 = 'I\'m a great coder!'
  2. str2 = "The quotation mark is \", the Chinese double quotation mark is ""
  3. print(str1)
  4. print(str2)

operation result:

I'm a great coder!
The quotation marks are ", and the Chinese double quotes are "

2) Use different quotes around the string

If single quotes appear in the string content, then we can use double quotes to surround the string, and vice versa. For example:

 
  1. str1 = "I'm a great coder!" #Use double quotes to surround a string containing single quotes
  2. str2 = 'Quotation double quotes are ", Chinese double quotes are "' #Use single quotes to surround strings containing double quotes
  3. print(str1)
  4. print(str2)

The running results are the same as above.

Newline of string

Python is not a free-form language. It has strict grammatical requirements for program line breaks and indentation. To wrap a longer string in a new line, you must add a backslash at the end of the line. \See the following example:

 
  1. s2 = 'It took me six months to write this Python tutorial. \
  2. Please give me more support. \
  3. I will keep it updated.'

The s2 string above is relatively long, so escape characters are used \to wrap the string content, so that a long string can be written into multiple lines.

In addition, Python also supports newline expressions, for example:

 
  1. num = 20 + 3 / 4 + \
  2. 2 * 3
  3. print(number)

Python long string

In the " Python Comments " section, we mentioned that you can use three single quotes or double quotes to comment multi-line content. This is actually the way to write long strings in Python. The so-called long string is \a string that can be written directly with line breaks (without adding backslashes).

Python long strings are surrounded by three double quotes """or three single quotes '''. The syntax format is as follows:

"""Long string content"""
'''Long string content'''

Placing single or double quotes within long strings will not cause parsing errors.

If the long string is not assigned to any variable, then the long string will not play any role and is no different from a piece of ordinary text, which is equivalent to being commented out.

Note that the Python interpreter will not ignore long strings at this time, and will parse them according to the grammar, but long strings will not play any practical role.

When there is a large piece of text content in the program that needs to be defined as a string, it is first recommended to use the long string form, because this form is very powerful and can place anything in the string, including single quotes and double quotes.

[Example] Assign a long string to a variable:

 
  1. longstr = '''It took me 6 months to write this Python tutorial.
  2. Please give me a to 'thumb' to keep it updated.
  3. The Python tutorial is available at http://c.biancheng.net/python/.'''
  4. print(longstr)

Newlines, spaces, indents and other whitespace characters in long strings will be output as they are, so you cannot write as follows:

 
  1. longstr = '''
  2. It took me 6 months to write this Python tutorial.
  3. Please give me a to 'thumb' to keep it updated.
  4. The Python tutorial is available at http://c.biancheng.net/python/.
  5. '''
  6. print(longstr)

Although the format is beautiful, the output will become:


    It took me 6 months to write this Python tutorial.
    Please give me a to 'thumb' to keep it updated.
    The Python tutorial is available at http://c.biancheng.net/python/.
 

There are two more blank lines before and after the string content, and there will be four more spaces in front of each line.

Python raw string

The backslash in Python strings has a special function, which is to escape characters, such as the and \mentioned above . We will explain it in detail in the " Python Escape Characters " section, but here we will briefly understand it. Escape characters sometimes cause some trouble. For example, if I want to represent a string containing a Windows path, writing it directly in a Python program will definitely not work, whether it is an ordinary string or a long string. Because of the particularity of , we need to escape each element in the string , that is, write it in this form. This kind of writing requires special caution, as a slight oversight can lead to errors. To solve the problem of escaping characters, Python supports raw strings. In the original string, they will not be treated as escape characters, and all content will remain "original". Add a prefix to the beginning of an ordinary string or a long string to turn it into an original string. The specific format is:\'\"

D:\Program Files\Python 3.8\python.exe\\D:\\Program Files\\Python 3.8\\python.exe

\

r

str1 = r'original string content'
str2 = r"""original string content"""


Rewrite the above Windows path into a raw string:

 
  1. rstr = r'D:\Program Files\Python 3.8\python.exe'
  2. print(rstr)

quotes in raw string

If quotation marks appear in the original string in ordinary format, the program also needs to escape the quotation marks, otherwise Python will still be unable to accurately match the quotation marks of the string; but unlike ordinary strings, the backslash used for escaping at this time The bar will become part of the string content.

Please look at the code below:

 
  1. str1 = r'I\'m a great coder!'
  2. print(str1)

Output result:

I\'m a great coder!


It should be noted that the backslashes in Python's original string will still escape the quotes, so the end of the original string cannot be a backslash, otherwise the quotes at the end of the string will be escaped, resulting in the string Can't end correctly.

There are two ways to solve this problem in Python: one way is to write long strings instead of raw strings; the other way is to write backslashes separately, which is what we will focus on next.

For example, if you want to express it D:\Program Files\Python 3.8\, you can write it like this:

 
  1. str1 = r'D:\Program Files\Python 3.8' '\\'
  2. print(str1)

We first wrote an original string r'D:\Program Files\Python 3.8', and then '\\'wrote a normal string containing escaped characters. Python will automatically splice the two strings together, so the output of the above code is:

D:\Program Files\Python 3.8\

Since this writing method involves knowledge about string splicing, readers only need to understand it here. String splicing will be introduced in detail later.

Guess you like

Origin blog.csdn.net/weixin_74774974/article/details/133420812