I used to read Python intermittently. Because there were few applications, I forgot to read it. So I recorded it here and checked it later. This is mainly to record the common modules in Python. The record part is just your own understanding.
The Python version is 2.5.2.
>>> Import sys
>>> SYS. Version
'2. 5.2 (r252: 60911, Feb 21 2008, 13:11:45) [MSC v.1310 32 bit (Intel)]'
>>> Import string
>>> Dir (string)
['Template', '_ templatemetaclass', '_ builtins _', '_ Doc _', '_ file __', '_ name _', '_ float', '_ idmap',' _ idmapl ',' _ int', '_ long',' _ multimap ', '_ re', 'ascii _ letters', 'ascii _ lowercase', 'ascii _ uppercase', 'atof ', 'atof _ error', 'atoi ', 'atoi _ error', 'atol ', 'atol _ error', 'capitalize', 'capword', 'center', 'Count', 'digits', 'expandtabs ', 'Find ', 'hdigits', 'index', 'index _ error', 'join', 'joins', 'Letters ', 'ljust', 'lower ', 'lowercase', 'lstrip', 'maketrans ', 'ocdigits', 'printable', 'punctuation', 'replace ', 'rfind', 'rindex', 'replace ust ', 'rsplit ', 'rdstrip', 'split', 'splitfields', 'strip', 'swapcase', 'translate', 'uppercase', 'white ', 'zfill']
All the methods and variables listed in Python are listed above. Let's take a look at the classification statistics.
Next we will classify and count the variables, functions, and classes in the string module.
>>> Funorc = []
>>> Vars = []
>>> For FV in Dir (string ):
Name = "string. % s" % Fv
If callable (eval (name )):
Funorc. append (Fv)
Else:
Vars. append (Fv)
>>> Funorc
['Template', '_ templatemetaclass', '_ float',' _ int', '_ long',' _ multimap', 'atof ', 'atof _ error ', 'atoi', 'atoi _ error', 'atol ', 'atol _ error', 'capitalize', 'capword', 'center', 'Count', 'expandtabs ', 'Find ', 'index', 'index _ error', 'join', 'joinfields', 'ljust ', 'lower', 'lstrip', 'maketrans ', 'replace ', 'rfind', 'rindex', 'Transport ust ', 'rsplit', 'rdstrip', 'split ', 'splitfields', 'strip', 'swapcase ', 'translate', 'uppper ', 'zfill']
>>> Vars
['_ Builtins _', '_ Doc _', '_ file _', '_ name _', '_ idmap ', '_ idmapl',' _ re', 'ascii _ letts', 'ascii _ lowercase', 'ascii _ uppercase', 'digits ', 'hdiges', 'Letters ', 'lowercase', 'digdigits ', 'printable', 'punctuation', 'uppercase', 'whiteid']
For variables, We can output their values one by one.
>>> For V in vars [6:]:
V, "===>", Eval ("string. % s" % V)
('_ Re',' ===> ', <module 'Re' from 'C: \ python25 \ Lib \ re. pyc'>)
('Ascii _ letters ',' ===> ', 'abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzxyz ')
('Ascii _ lowercase', '===>', 'abcdefghijklmnopqrstuvwxy ')
('Ascii _ uppercase', '===>', 'abcdefghijklmnopqrstuvwxy ')
('Digits ',' ===> ', '123 ')
('Hexdigits ',' ===> ', '0123456789abcdefabcdef ')
('Letters ',' ===> ', 'abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxy ')
('Lowercase', '===>', 'abcdefghijklmnopqrstuvwxy ')
('Octal digits ',' ===> ', '123 ')
('Printable', '===>', '0123456789abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz! "# $ % & \ '() * +,-./:; <=>? @ [\] ^ _ '{| }~ \ T \ n \ r \ x0b \ x0c ')
('Punctuation', '===> ','! "# $ % & \ '() * +,-./:; <=>? @ [\] ^ _ '{| }~ ')
('Uppercase', '===>', 'abcdefghijklmnopqrstuvwxy ')
('Whiteidspace ',' ===> ',' \ t \ n \ x0b \ x0c \ R ')
Let's look at the functions in the form of examples.
String. capitalize (s) returns a copy of string S. the first character of this copy is capitalized.
>>> S = "Hello World"
>>> String. capitalize (s)
'Hello world'
String. capwords (s) the first letter of each word is capitalized.
>>> String. capwords (s)
'Hello world'
You can also see its implementation through help (string. capwords. Use split to split, capitalize to uppercase, and join to connect.
Implement the following by yourself:
>>> Def mycapwords (ARG ):
Newstring = []
Strlist = Arg. Split ()
For STR in strlist:
Newstring. append (Str. capitalize ()))
Return string. Join (newstring)
>>> String. Center (S, 20)
'Hello world'
>>> String. Center (S, 2)
'Hello world'
>>> String. Center (S, 20 ,'*')
'***** Hello World *****'
String. Center (S, width [, fillchar]) function, returns a center version of s with the specified width. If necessary, fill it with fillchar. The default is space. However, S is not intercepted. That is, if the length of S is greater than the width, S is not intercepted.
>>> String. Count (S, "H ")
1
String. Count (S, sub [, start [, end]) returns the number of times that sub-strings appear in string s in the range of S [start: end ].
>>> String. Find (S, "")
-1
String. find (S, sub [, start [, end]) returns the minimum subscript of the substring sub in the range of S [start: end] In the string S.-1 is not found.
String. Index (S, sub [, start [, end]) is similar to the string. Find method. However, if the Sub-string is not found, a valueerror exception is thrown.
>>> String. Index (S, "")
Traceback (most recent call last ):
File "<pyshell #233>", line 1, in <module>
String. Index (S, "")
File "C: \ python25 \ Lib \ string. py", line 326, in Index
Return S. Index (* ARGs)
Valueerror: substring not found
>>> String. Ljust (S, 20)
'Hello world'
String. Ljust (S, width [, fillchar]) left alignment of the string,
String. Must ust () is the right alignment.
>>> String. Upper (s)
'Hello world'
>>> String. Lower (s)
'Hello world'
String. Upper () and string. Lower () are relatively simple. Converts all data to uppercase or lowercase letters.
>>> String. swapcase (s)
'Hello world'
String. swapcase () to convert the case. Converts uppercase to lowercase and lowercase to uppercase.
>>> S = "Hello World"
>>> String. Strip (s)
'Hello world'
String. Strip (s) removes spaces around string s
>>> String. lstrip (s)
'Hello world'
>>> String. rstrip (s)
'Hello world'
String. lstrip (s) and string. rstrip (s) Remove spaces on the left and right of the string respectively.
>>> String. zfill (S, 20)
'2017 Hello world'
>>> String. zfill (S, 2)
'Hello world'
String. zfill (S, width) is similar to that of center, but "0" is used for filling.
S = "ABC"
>>> X = string. maketrans (string. ascii_letters, String. ascii_letters [2:] + String. ascii_letters [: 2])
>>> String. Translate (S, X)
'Cde'
String. maketrans () and string. Translate () are generally used in combination. Use maketrans to define the conversion rules of strings, and then use translate to implement them.
We can use it to implement the swapcase () method.
>>> X = string. maketrans (string. ascii_letters, String. Letters)
>>> String. Translate ("abcdef", X)
'Abcdef'
>>> String. Translate ("AB CDE F", X)
'AB CDE F'
>>> String. Split ("Hello World ")
['Hello', 'World']
String. Split (S, SEP = none, maxsplit =-1) split S with SEP and return the list after split. If SEP is not provided or is none, the default space is used.
The function of string. Join is the opposite.
>>> L = string. Split ("Hello World ")
>>> String. Join (l)
'Hello world'
Join (list [, SEP]) combines the list into a string using Sep to return.