About the split and join methods of string
Os.path.splie ()/os.path.join () for importing OS modules seems to be a different processing mechanism, but functionally identical.
1.string.split (str= ", Num=string.count (str)): delimited by str, the character slices a string, and if NUM has a specified value, only the NUM substring is delimited.
S.split ([Sep [, Maxsplit]])-a list of strings separated by a string that returns a list of delimited (Sep) split strings. If you specify the maximum number of splits, the end is the maximum split.
If the delimiter is not specified or is none, the delimiter defaults to a space.
Note: Delimiters cannot be empty, otherwise an error occurs, but there can be delimiters that do not contain them.
Os.path.split ()
Os.path.split is separated by the path of the file name and path, such as D:\\python\\python.ext, can be divided into [' D:\\python ', ' Python.exe ']
The code is as follows:
Import OS
Print Os.path.split (' C:\\Program File\\123.doc ')
Print Os.path.split (' C:\\Program file\\ ')
-----------------Output---------------------
(' C:\\Program File ', ' 123.doc ')
(' C:\\Program File ', ')
2.string.join (Sep): Combines all elements (string representations) in Sep into a new string, using string as the delimiter.
Concatenate all the elements of a string, Ganso, and list in a join into a new string (String, Ganso, list They are sequence types, have the same access mode)
Os.path.join (path1[,path2[,......]]) returns a combination of multiple paths, and the parameters before the first absolute path are ignored.
The code is as follows:
>>> os.path.join (' c:\\ ', ' csv ', ' test.csv ')
' C:\\csv\\test.csv '
>>> os.path.join (' Windows\Temp ', ' c:\\ ', ' csv ', ' test.csv ')
' C:\\csv\\test.csv '
>>> os.path.join ('/home/aa ', '/home/aa/bb ', '/home/aa/bb/c ')
'/home/aa/bb/c '
Example:
Write a function, the argument is a long string and a word, the long string is word to the corresponding letter number of * *, for example, the long string of "this hack is wack hack", Word for "hack", then requires the function output: "This * * * is Wack * * * * "
The code is as follows:
def censor (Text,word):
Texts = Text.split ("")
For I in range (len (texts)): if texts[i] = = Word:
Texts[i] = "*" * LEN (Word)
Return "". Join (texts)
Print censor ("Hey hey Hey", "Hey")
Output:
*** *** ***