About the split and join methods for string
Os.path.splie () for importing an OS module ()/os.path.join () looks like a different processing mechanism, but functionally.
1.string.split (str= ', Num=string.count (str)): delimited by str, character slicing string, only num substrings delimited if NUM has a specified value.
S.split ([Sep [, Maxsplit]])-> A string-separated list returns a set of lists that are formed by using a separator (SEP) Split string. If the maximum split number is specified, it ends at the maximum partition.
If the delimiter is not specified or none, the delimiter defaults to a space.
Note: The separator cannot be empty, otherwise there will be an error, but you can have delimiters that do not contain them.
Os.path.split ()
Os.path.split is the path to split the file name and path, such as D:\\python\\python.ext, can be divided into [' D:\\python ', ' Python.exe ']
Copy Code code 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): Merges all elements of the SEP (string representation) into a new string as a delimiter.
Concatenate the strings, Ganso, and all the elements of a list into a new string (String, Ganso, list They are sequence types, with the same access)
Os.path.join (path1[,path2[,......]]) combines multiple paths and returns the arguments before the first absolute path are ignored.
Copy Code code 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 with a long string and a word that converts a long string of word to the number of letters, for example, a long string of "this hack is wack hack" and word is "hack", then the function output is required: "This * * is Wack * * * * * *
Copy Code code 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:
*** *** ***