Basic data types for Python
integer int
Float type float
truth bool
String str
List #[1,2,3]
Tuples Tuple # (All-in-each)
Dictionary Dict #{1:2}
Collection set #set {[' A ', ' C ', ' B ']} The collection object is an unordered set of hashed values: a member of a collection can make a dictionary key
String manipulation methods
1, Case transform
>>>a ="HEllo">>>a.lower ()#lowercase'Hello'>>>a.upper ()#Uppercase'HELLO'>>>a.swapcase ()#Swap Case'HeLLO'>>>a.title ()#title string, all words start with uppercase letters, other lowercase'Hello'>>>a.capitalize ()#first letter uppercase, remaining lowercase'Hello'
2. String arithmetic
+ String Connection
' Hello ' ' World ' Print (A +b) ' HelloWorld '
* Repeat Output
' ABC ' Print (a*3) ' ABCABCABC '
[] Index get character
' Hello ' Print (a[2]) L Print (A[0:2]) he
In member operator, returns TRUE if the string contains the given character
The not in member operator returns TRUE if the string does not contain the given character
R/R original string, no escape special or cannot print
Print (R'\nabc\n') \nabc\n Print (R'\nabc\n') \nabc\n
% format string
3. Format string
%c formatted character and its ASCII code
%s formatted string
%d formatted integer
%u format unsigned integer
%o Formatting an unsigned octal number
%x format unsigned hexadecimal number
%x format unsigned hexadecimal number (uppercase)
%f formatting floating-point numbers to specify the precision after the decimal point
%e format floating-point numbers with scientific notation
%E function with%E, using scientific notation to format floating-point
%g depends on the size of the value to use%f or%e
The%G function is the same as%G, depending on the size of the value, using%f or%e
%p the address of a variable with hexadecimal number format
Format Operator Helper:
* Define width or decimal precision
-Used to align Left
+ Positive front display + sign
<sp> positive spaces appear before
#八进制前显示 (' 0 '), which displays ' 0x ' or ' 0X ' before hexadecimal, depending on X or X
0 display numbers preceded by ' 0 ' instead of the default space
% ' percent ' output a single '% '
(VAR) mapping variable (dictionary parameter)
M.N m Displays the minimum width, n digits after the decimal point
4, three quotation marks
"" "three quotation marks allow a string to span multiple lines, and the string can contain line breaks, tabs, and other special characters
5. Unicode string
>>> u'hello\u0020world! ' u ' Hello World! '
inserting Unicode characters (whitespace) encoded with a value of 0x0020
6. The built-in function of the string
' Hello World '>>> s.find ('el')1
>>> s.find (' El ', 0, 3)
1
Returns the first letter designator of the substring that appears, if no 1 is returned;
S.find (substr,beg,end) returns the index value contained within the specified range of Beg end
' This is a test '. Replace ('is ''eez') ' theez EEZ a test '
Returns the string from which all occurrences of a string are replaced
>>>'1+2+3+4+5'. Split ('+') ['1','2','3','4','5'] >>>'/usr/bin/env'. Split ('/') ["','usr','bin','Env'] >>>'Using the default'. Split () ['Using',' the','default']
Splitting a string into a sequence
>>> seq = ['1','2','3','4','5'] >>> Sep ='+'>>>sep.join (seq)'1+2+3+4+5'>>> dirs ="','usr','bin','Env'>>>'/'. Join (dirs)'/usr/bin/env'
Join adds elements to the list
' internal whitespace is kept '. Strip () 'Internal Whitespace is kept'
Strip removing strings with both sides (without internal) spaces
S.join
S.center (width)
S.count (Str,beg,end)
S.index (str,beg,end) #和find方法一样, str is not reported abnormally
S.isalnum () #至少一个字符且所有字符为字母或数字返回True
S.isdecimal ()
S.islower ()
S.isnumeric ()
S.isspace ()
S.istitle ()
S.isupper ()
S.digits: The string of the replacement number 0-9
S.letters: String containing all letters (uppercase and lowercase)
S.lowercase: A string containing all lowercase letters
S.printable: A string containing all printable characters
S.punctuation: A string containing all punctuation
S.uppercase: A string containing all uppercase letters
PS: The method of string is very many, need to master in the instance
Python Learning note 01 python data type