Python Split and Stitch strings

Source: Internet
Author: User
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:
*** *** ***
  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.