Single and double quotes in python strings

高洛峰
Release: 2017-02-18 10:05:45
Original
1358 people have browsed it

Strings in python can (and can only) be surrounded by pairs of single quotes, double quotes, and three double quotes (document strings):

'this is a book'
" this is a book"
"""this is a book"""

can contain double quotes, triple quotes, etc. in a string surrounded by single quotes, but it cannot contain single quotes itself (needs to be converted Meaning)

'this is a" book'
'this is a"" book'
'this is a""" book'
'this is a\' book'

You can also escape double quotes in single quotes, but it is usually not necessary or meaningful.

'this is a\" book'

Similarly, double quotes can be escaped Contains single quotes, but cannot contain double quotes and triple quotes composed of double quotes

"this is a' book"
"this is a\" book"

can also be used Escape the single quotes within the double quotes, but again, this is usually not necessary or meaningful

"this is a\' book"

Now one more question, if I want to When "\'" is displayed in a string surrounded by single quotes, the answer is to escape "\" and "'" respectively. That is, if you want to display the special character "\" in a string, you need to escape the special character itself. Escape, similar to other special characters.

>>> s='this is a\' book'
>>> print s
this is a' book

>> ;> s='this is a\\\' book'
>>> print s
this is a\' book

How many times do you want to display "\" How many times to escape "\":

>>> s='this is a\\\\\' book'
>>> print s
this is a\\' book


Similarly, if you want to display "\"" in a string surrounded by double quotes, you must escape "\" and """ respectively.

>>> s="this is a\\\" book"
>>> print s
this is a\" book

Having said that, it is necessary to talk about the replacement of "\'" and "\"" in the string, that is, the string itself contains such substrings, such as:

>> > s='this is a\\' book'
>>> s
"this is a\\' book"
>>> print s
this is a\' book


The string here contains a substring like "\'", and now I want to replace this substring with "@@@"
>> > s=s.replace('\\\'','@@@')
>>> s
'this is a@@@ book'
>> > print s
this is a@@@ book

That is, when writing the substring to be replaced, special characters need to be escaped, s=s.replace('\ \\'','@@@'), the substring that will be replaced in the final string is "\'".

Substrings containing special characters in double quotes The replacement follows the same principle.

In addition, it should be noted that if you want to know the final appearance of the string, you should use the print function to print it out to avoid confusion. > s='this is a\\' book'

>>> s

"this is a\\' book"
>>> print s
this is a\' book

The single and double quotes in the python string above are all the content shared by the editor. I hope it can give you a reference, and I hope you will support the PHP Chinese website.

For more articles related to single and double quotes in python strings, please pay attention to the PHP Chinese website!

##
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!