Python split and concatenation string _python

Source: Internet
Author: User
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:
*** *** ***
Related Article

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.