Four expressions of strings in python

Keywords: Python

Today, I'm learning the basic contents of python, and how to operate strings in Python

It mainly refers to several expressions of strings in python.

Several expressions of python

1 enclose a string in single quotes

>>> 'my python lession'       #To enclose a string in single quotes
'my python lession'
>>> a = 'my python lession'   
>>> print(a)
my python lession

2 use double quotes to expand the string

>>> "my python lession"    #Using double quotes to expand a string
'my python lession'
>>> a = "my python lession"
>>> print(a)
my python lession

PS: No one answers the questions? Need Python Learning materials? You can click the link below to get it by yourself
note.youdao.com/noteshare?id=2dce86d0c2588ae7c0a88bee34324d76

3 when you want to output single quotation mark or double quotation mark (output single quotation mark and double quotation mark as common characters), escape by

>>> 'python \'escape'           
"python 'escape"
>>> a = 'python \'escape'
>>> print(a)
python 'escape     #Use the \ backslash to transfer the single quotation mark, regardless of whether the outer layer is the single quotation mark or the double quotation mark. Anyway, the middle is the string. If you have the \ back, use the single quotation mark and the double quotation mark to escape
>>> 
>>> 'python \" escape'
'python " escape'
>>> a = 'python \" escape'
>>> print(a)
python " escape
>>> 
>>> "python \' escape"
"python ' escape"
>>> a = "python \' escape"
>>> print(a)
python ' escape
>>> 
>>> "python \" escape"
'python " escape'
>>> a = "python \" escape"
>>> print(a)
python " escape

4. Output single quotation mark and double quotation mark by mixing single quotation mark and double quotation mark

>>> '"double quote"'    #In single quotation marks, use double quotation marks to output double quotation marks directly
'"double quote"'
>>> a = '"double quote"'
>>> print(a)
"double quote"
>>> 
>>> "'single quote'"     #In double quotes, use single quotes to output single quotes
"'single quote'"
>>> a = "'single quote'"
>>> print(a)
'single quote'
>>> 
>>> ""double""   #Output double quotes directly in double quotes to report errors
SyntaxError: invalid syntax
>>> "\"double\""  #The double quotation mark is output directly in the double quotation mark, but the double quotation mark can be escaped through the backslash
'"double"'
>>> a = "\"double\""
>>> print(a)
"double"
>>> 
>>> ''single''  #Single quotation mark in the output single quotation mark character error, plus escape character is OK.
SyntaxError: invalid syntax
>>> '\'single\''
"'single'"
>>> a = '\'single\''
>>> print(a)
'single'
>>> '"My python', lession'  #Double quotation marks can be output in single quotation marks. If there are single quotation marks, they must be escaped
SyntaxError: EOL while scanning string literal
>>> '"My python\', lession'
'"My python\', lession'
>>> a = '"My python\', lession'
>>> print(a)
"My python', lession

Note: in the interactive interpreter, the output strings are all enclosed by quotation marks. If the output has single quotation marks, the output strings will be enclosed by double quotation marks. If the output strings have double quotation marks, the output strings will be enclosed by single quotation marks. If the output strings do not have single quotation marks, double quotation marks, the output strings will be enclosed by single quotation marks.

Posted by peterg0123 on Wed, 25 Dec 2019 09:35:43 -0800