Integers (int)
Integers (int) How to write it
num = 1234 # Positive integer
0o(0O), 0x(0X), 0b(0B) They are octal , Hexadecimal , Binary number .
num = 0o777 # octal
num = 0xffff # Hexadecimal
num = 0b11000100 # Binary system
long integer (long)
Long integer (long) Than (int) Wide range integer . Add... At the end L. The maximum value of an integer passes through (sys.maxint) obtain 、231-1 , 263-1 , And so on .
num = 9223372036854775808L
num = 1234567890123456789012345678901234567890123456789012345678901234567890L
Python 3 after 、 Integers (int) And long integers (long) Unified as (int),L and l The writing of has been abolished .
num = 123L # Python 3 Can report grammatical errors
Floating point decimal (float)
Floating point decimal (float) Let's write it as follows .e2 Express 10 Of 2 The power of .
num = 1.234 # Floating point decimal
num = 1.2e3 # Floating point decimal notation 1.2 × 103
num = 1.2E-3 # Floating point decimal notation 1.2 × 10-3
imaginary number (complex)
imaginary number (complex) use j perhaps J To express .
num = 3.14j
Boolean value (bool)
Boolean value (bool),True perhaps False. The first letter is capitalized .
bool = True
bool = False
stay Python in ,False、 Numerical 0 and 0.0、 Null character ("")、 An empty array ([])、 empty tumble(())、 An empty dictionary ({}) And so on can be equated with False, Beyond these are True.
character string (str)
character string (str) Use double quotes ("), Single quotation marks (').
str = "Hello world"
str = 'Hello world'
"..." You can use single quotation marks in '、'...' You can use double quotation marks in "."..." Use double quotes in ",'...' If there is a single quotation mark in the 、 Use escape characters (\) To express .
str = "We can use \" in the string."
str = 'We can use \' in the string.'
The backslash (\) It is used to indicate that it is divided into multiple lines .
str = 'Hello \
world!'
"..." and '...' It says in front of r perhaps R, The escape of backslash inside double quotation marks is invalid .
str = 'aaa\nbbb' # \n This is the newline character
str = r'aaa\nbbb' # \n Here are backslashes and n
Three consecutive double quotes """...""" Or single quotes '''...''' , Represents a multiline comment .
str = """A simple example module
This module is ...
"""
Multiple strings are separated by spaces , Represents string splicing .
print 'Hello ' 'world!'
Unicode character string (unicode)
Unicode character string (unicode) Contains ASCII Of other types Unicode A string of words .Python 2 Next ,"..." and '...' It is mandatory to add u perhaps U.Unicode character string (unicode), The number of words will be calculated in unicode In units of account ..
# Python 2
len = len(u' Test string ') # 5 A word
len = len(' Test string ') # 15 A word
# Python 3
len = len(u' Test string ') # 5 A word
len = len(' Test string ') # No, u It will also serve as 5 Characters
Invalid escape character , Appoint u perhaps UR.
print ur' character string \n'
byte Column (bytes)
bytes = b'0123456789abcdef'
Escape character (\x)
The following escape characters can be used in the string ..
Escape character
\ Diverted : Invalid escape character
\\ : Escape character (\)
\' : But quotation marks (')
\" : Double quotes (")
\n : Diverted (LF)
\r : recovery (CR)
\t : tab(TAB)
character string format(%)
errmsg = "Can't open file"
errcode = 19042
msg = "ERROR: %s (%d)" % (errmsg, errcode)
print msg #=> ERROR: Can't open file (19042)
%s character string 、%d Integers 、%f Floating point decimal 、%x Hexadecimal 、%o octal 、%% Oneself
print "%s" % "ABC" #=> ABC
print "%d" % 123 #=> 123
print "%f" % 1.23 #=> 1.23
print "%x" % 255 #=> ff
print "%o" % 255 #=> 377
print "%%%d" % 80 #=> %80
print "|%5s|" % 'ABC' #=> | ABC| : To the right 5 Colombian script .
print "|%-5s|" % 'ABC' #=> |ABC | : Move to the left 5 A word
print "|%5d|" % 123 #=> | 123| :
print "|%-5d|" % 123 #=> |123 | :
print "|%+5d|" % 123 #=> | +123| :
print "|%5.2f|" % 1.23 #=> | 1.23| :
print "|%05d|" % 123 #=> |00123| :