Built-in function--string method __ function

Source: Internet
Author: User
One, string method1. Strictly speaking, the function is not called the method. The object method used to process objects is easy to understand and can be understood as functions.

2. View the string method, you can use the TAB key to view.


(1). Capitalize method, capitalize the first letter.

(2). Replace method replaces the inside character. There are 2 parameters, the first argument is old, and the second is the new string.

You can write a value later and replace it several times.

In fact, if you want to see what methods, do not define a variable, you can directly use Str. Look, the way to view a list is to use list. Ganso with tuple. (3). Split method, cutting string, you can customize the cutting character. To cut a string into a list

Custom cut symbol, custom cut several times.

(4). Join method, connect 2 iteration objects
Use range to connect
Second, string module to use the string module, first import string. (1) string.lowercase print all lowercase letters.

(2) string.uppercase print all uppercase letters

(3) String.capitalize capitalize the first letter. The same as the string caplitalize function
(4) String.Replace replacement string 3 required parameters an optional parameter, the first is the string to be replaced, the second is the value to be replaced, the third is the new value, and the optional argument is, replace several times.
In addition to Len (), Max (), Min () function, there are several advanced functions in the sequence processing function . (1) the filter () function is used for filtering sequences, filtering out elements that are not eligible, and returning a new list of qualified elements. This receives two parameters, the first is a function, the second is a sequence, each element of the sequence is passed as a parameter to the function, then returns TRUE or False, and finally the element that returns true is placed in the new list. Def a (n): return n% 2 = 1 Print filter (A,range (10))
Filter out even numbers with filter. If you use anonymous functions, you cannot add if filter (lambda x:x%2 ==0, Range (10)) so write it.
(2) the zip () function uses an object that can be iterated as a parameter, packs the corresponding elements in the object into a tuple, and then returns a list of those tuples. If the number of elements in each iterator is inconsistent, the return list is the same length as the shortest object, and the * operator allows you to extract the tuple into a list.
The ZIP function is equivalent to a zipper, combining 2 sequences into a single list, and then one by one correspondence by subscript. Can be created by Zip to convert to dictionary.

If the length of the sequence is not the same. Zip will take the shortest.

(3) Map () maps the specified sequence based on the function provided. The first parameter function calls the function function with each element in the parameter sequence, and returns a new list that contains the return value of each of the function functions.

The third element is populated with none. Defines the powers, L1 each element.

You can also do def f (n) on multiple sequences: # Compute the square number return n + N map (f, [1,2,3,4,5])

Map (Lambda x:x * * 2, [1, 2, 3, 4, 5]) # using lambda anonymous functions

# provides two lists that add to the list data in the same position map (lambda x, y:x + y, [1, 3, 5, 7, 9], [2, 4, 6, 8])

When more than one sequence is defined, the number of arguments is. (4) The reduce () function accumulates elements in a parameter sequence. function to set a data collection (linked list, tuples, and so on): Use the function function (with two arguments) passed to reduce to manipulate the 1th and 2 elements in the set, and the results are then performed with the third data with the functions, and finally a result is obtained. (1) The reduce function is a built-in function of Python, and his role is to do a two-dollar calculation of the defined function and value. (2) Reduce syntax, reduce (function, operator, sequence) function must be a function containing 2 parameters, 1 or extra 2 are not. The following sequence is optional. See Example reduce (Fun,range (1,10))

The general definition of a function, and finally write the sequence, will automatically compute the class. 1+2+3+4+5+6+7+8+9 's and. The lambda function defines reduce (lambda x, y:x + y,range (1,10))

third, List of expressions [I*2+10 for I in range (10)]

[I for I in range (Ten) if i%3 ==0]

This way is similar to using filter and map.

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.