Split && Join && Append

Source: Internet
Author: User

These three methods of personal feeling is very practical, and very similar, so put together:

Split && Join      
  Split Note 1 NOTE 2
  Function: Splits a string; Returns a list 1. You can split characters that do not exist in the string and return to the full list:
string = ' Sadhjfhdj '
Print String.Split ("5")
>>>[' SADHJFHDJ ']
But you can't leave the separator blank!
    2. Can be used in conjunction with various methods of the list, such as slicing  
    3. You can add a parameter to determine how many splits are made:
string = ' 121212121 '
Print String.Split ("2", 1)
>>>[' 1 ', ' 1212121 ']
 
       
  Os.path.split () Ex:
Import OS
Print Os.path.split ("C://documents and Xxx/junzhou/what/file.txt")
>>> (' c://documents and Xxx/junzhou/what ', ' file.txt ')
 
  Function: Divides the path from the file name; returns a tuple    
       
join ex:
A = (' A ', ' B ', ' C ', ' d ')
print ". Join (a)
>>>ABCD
b = [' 1 ', ' 2 ', ' 3 ', ' 4 ']
print '. Join (b)
>>>123456
C = ' 123456 '
print '. '. Join©
>>>1.2.3.4.5.6
  Os.path.join () Ex
print os.path.join (' windows\temp ', ' c:\\ ', ' csv ', ' test.csv ')
>>>c:\csv\test.csv

Print Os.path.join (' c:\\ ', ' csv ', ' test.csv ')
>>>c:\csv\test.csv
Parameters before the first absolute path will be ignored!

Append

Append is the list method that will add elements to the list tail:

A = [' 1 ', ' 2 ', ' 3 ']
A.append (4)
Print a

>>>[' 1 ', ' 2 ', ' 3 ', ' 4 ']

Here's a little bit to do with the join: Append is for list, so no matter what list can be implemented, and join only for the STR type, if you need to connect to a list, each item in this list must be str type

Such as:

A = [1,2,3,4]

print '. '. Join (a)

>>> Error exception because the item in list A is of type int

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.