[Python learning notes 01] python data type, python learning notes
Basic python Data Types
Integer int
Float
Boolean
String str
List # [1, 2, 3]
Tuple # (1, 2, 3)
Dictionary dict}
Set # set {['A', 'C', 'B']} collection objects are a group of unordered hash values: set members can be used as Dictionary keys.
String operation method
1. Case sensitivity
>>> A = "HEllo" >>>. lower () # lower case 'hello' >>>. upper () # upper case 'hello' >>>. swapcase () # swap case 'hello'>. title () # title string. All words start with uppercase letters, and other lowercase 'hello' >>>. capitalize () # upper-case letters, and lower-case 'hello'
2. String operations
+ String connection
>>> a = 'hello'>>> b = 'world'>>> print(a+b)'helloworld'
* Repeated output
>>> a = 'abc'>>> print(a*3)'abcabcabc'
[] Index obtaining characters
>>> a = 'hello'>>> print(a[2])l>>> print(a[0:2])he
In member operator, returns True if the string contains a given character
Not in member operator, returns True if the string does not contain the given character
The original r/R string. It is not escape special or cannot be printed.
>>> print(r'\nabc\n')\nabc\n>>> print(R'\nabc\n')\nabc\n
% Format String
3. Format String
% C formatted characters and their ASCII code
% S Format String
% D format integer
% U format unsigned integer
% O Format the unsigned octal number
% X format the unsigned hexadecimal number
% X format the unsigned hexadecimal number (uppercase)
% F format a floating point number to specify the decimal point Precision
% E format the floating point number using scientific notation
% E is used in the same way as % e. The floating point is formatted using scientific notation.
% G determines whether to use % f or % e Based on the value size.
% G is the same as % g. % f or % e is used according to the value size.
% P format the address of the variable in hexadecimal notation
Formatting operator assistance:
* Defines the width or decimal precision.
-Left alignment
+ Front of positive number + sign
<Sp> A space is displayed before a positive number.
# Display before octal ('0'), '0x 'or '0x' before hexadecimal, depending on x or X
0: Fill '0' in front of the displayed number instead of the default space.
% 'Output a single' %'
(Var) ing variable (Dictionary parameter)
M. n m display minimum width, n digits after decimal point
4. Three quotation marks
''' Three quotation marks allow a string to span multiple lines. A string can contain line breaks, tabs, and other special characters.
5. Unicode string
>>> u'Hello\u0020World !'u'Hello World !'
Insert a Unicode character (space character) with the encoding value 0x0020)
6. String built-in functions
>>> s = 'hello world'>>> s.find('el')1
>>> s.find('el',0,3)
1
Returns the first letter number of a substring. If no value is returned,-1 is returned;
S. find (substr, beg, end) returns the index value within the specified range of beg end.
>>> 'This is a test'.replace('is', 'eez') 'Theez eez a test'
Returns the string obtained after all matching items of the 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']
Splits 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 to add elements to the list
>>> ' internal whitespace is kept '.strip() 'internal whitespace is kept'
Strip removes strings with spaces on both sides
S. join
S. center (width)
S. count (str, beg, end)
S. index (str, beg, end) # Same as the find method, str does not report an exception
S. isalnum () # returns True if there is at least one character and all characters are letters or numbers
S. isdecimal ()
S. islower ()
S. isnumeric ()
S. isspace ()
S. istitle ()
S. isupper ()
S. digits: A string with a number ranging from 0 to 9
S. letters: string containing all letters (uppercase and lowercase)
S. lowercase: string containing all lowercase letters
S. printable: string containing all printable characters
S. punctuation: string containing all punctuation
S. uppercase: string containing all uppercase letters
Ps: There are many string methods that need to be mastered in the instance