Strings (single quotation marks double quotation marks triple quotation marks) and the operation of strings and numbers in python3

  • 2021-07-22 10:03:21
  • OfStack

Strings in python3 are a common data type.

Strings have multiple representations: single quotation marks, double quotation marks, and 3 quotation marks, and the representations of these strings (single, double, and 3) must all appear in pairs.

Single and double quotation marks are English: ''and'', while 3 quotation marks are 3 single quotation marks or 3 double quotation marks can be either: '','' or ''''. There is no order when typed in python (in fact, you can't see the order...). If you type something in the middle of a pair of quotation marks, it is a string, for example:


# Single quotation mark #
'123' ' Xiao Ming ' 'xyz'
 
# Double quotation marks #
"123" " Xiao Ming " "xyz"

About single quotation marks:

Single quotation marks are special because in English a lot of syntax is abbreviated, for example: what is your name? And what 's your name? But in python3, what 's your name if it is a single quote string? You will make mistakes.

Because python will not recognize your English syntax, but will recognize whether your code syntax is correct. If you write 'what' s your name directly? 'python will think' what 'is a string, followed by s your name? 'Is an incorrect string.

The correct way is to use double quotation marks. However, single quotation marks can also achieve what 's your name? As a string, it is implemented by using an escaped character:\ (yes\ no/, don't get the wrong direction), as follows:


# Double quotation marks #
>>> "what's your name?"
"what's your name?"
 
# Single quotation mark with escape character #
>>> 'what\'s your name?'
"what's your name?"

About 3 quotation marks:

3 quotation marks can be used to write code in new lines (sometimes the code is too long, and the new lines will be easy to understand and read). Directly type 3 single quotation marks or double quotation marks (it must be a pair, not a single quotation mark at the beginning and a double quotation mark at the end), and output in new lines, which can be used at the end of 3 quotation marks.

However, the output result will be a 1-line character band\ n, and the\ n in the final result represents carriage return, because you typed carriage return in the new line input. Although you can't see it by typing carriage return, it is indeed a 1-character input, so it is represented by\ n.

In addition, the string with single and double 3 quotation marks can also be newline, and you only need to add\ directly at the newline to enter the newline.

One more point about escape characters: Simply use print + escape character\ n, so that the output is divided into several lines and\ n is not displayed. But the code is still 1 line. As follows:


#print+ Escape character \n Output results wrap #
>>> print ('123\n456\n789')
123
456
789
 
>>> print ("123\n456\n789")
123
456
789
 
>>> print ('''123\n456\n789''')   
123
456
789
 
>>> print ("""123\n456\n789""")   
123
456
789
 
 
#\ Line break (single and double 3 Quotation marks all 1 Sample, so just show 1 Single quotation marks) #
>>>'123\
123\
123'  
'123123123'
 
 
#3 Quotation mark wrapping #
>>> '''
123
456
789
'''
'\n123\n456\n789'
 
>>> """123
456
789"""
'123\n456\n789'
 
# And 1 Point, 3 Quotation marks and append \ Single and double quotation marks of, no matter where the quotation marks are, as long as they contain characters. #

Note that in python, the string 123 is different from the number 123. If you put the number 123 +456, you will get the result 579.

If you add 123 of the string to 456 of the string, the two strings will be merged.

However, if you put the string 123 + the number 456, an error will be reported, as follows:


# Numbers plus numbers #
>>> 123+456
579
 
# Character plus character #
>>> '123'+'456'
'123456'
 
# Adding characters to numbers #
>>> '123'+456
Traceback (most recent call last):
 File "<pyshell#2>", line 1, in <module>
  '123'+456
TypeError: can only concatenate str (not "int") to str

Numbers can't be added to strings, but they can be multiplied! The string * means that this string is output several times in total. As shown below:


# In python Medium * Represents multiplication, / Represents division that preserves the decimal point, // Represents a division that preserves only integers, + For addition,-for subtraction #
 
>>> '123'*3
'123123123'
 
>>> ' Something important '*3
' Important things important things important things '

But strings and strings cannot be multiplied.


Related articles: