Python split string, filter function usage, pythonsplit

Source: Internet
Author: User

Python split string, filter function usage, pythonsplit

For an existing string, you need to extract the first segment separated by spaces. The operation is as follows:

>>> Product_model = 'ws-C2960G-24TC-L-Fixed Module 0 ′
>>> Product_model.split ('') [0]
'Ws-C2960G-24TC-L'

Split () without parameters, all spaces (space characters, tabs, line breaks) are used as separators. If these spaces exist, you can write them like this.

>>> Product_model = 'ws-C2960G-24TC-L-Fixed Module 0 ′
>>> Product_model.split () [0]

Filter is used to filter data.

>>> Product_model = 'ws-C2960G-24TC-L-Fixed Module 0 ′
>>> Filter (None, product_model.split ('-'))
['Ws ', 'c2960g', '24tc', 'l', 'fixed Module 0']

Article from: http://www.111cn.net/phper/python/64510.htm


Python split () is separated by spaces by default. Now there are # and space in the string. How can split only by #, not by space?

The use of split () is like this. You only need to add in the brackets What You Need To split. For example, if you want to split with #, then split ("#"). For example, a = "a # a", B = a. split ("#") >>> print B to output [a, a, a]

Use of split in python

The default delimiter of the string split function is space''
If no separator exists, the entire string is used as an element in the list.

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.