Which of the following function will return a list containing all the words of the given string?

Answers [ ]

  1. Option c : partition[] function always return a tuple of 3 elements.


    More about functions :

    a] find[] : The find[] function returns the index of the first occurrence of the substring is found. If not found, then it returns -1.

    Example :

    str = “I love car.”

    index = str.find[‘love’]

    print[“Substring ‘love’ : “, index]

    b] index[] : The index[] function returns the index of the given element in the list.

    Example :

    L = [10 , 12 , 23 , 34 , 45]

    index = L.index[12]

    print[“Index of 12 is “,index]

    c] partition[] : The partition[] function splits the string at the first occurrence of the given string and always return a tuple containing three parts :

    1. String before separator
    2. Given string
    3. String after separator.

    str = “I live in Lucknow.”

    print[str.partition[‘in’]

    d] split[] : The split[] function returns a list of the words in the string/line, separated by a delimiter string.

    Example :

    str = “I love to play football.”

    print[str.split[” “,3]]

  2. B]Index

    Hope it will help you

sort[] method

The sort[] method is a built-in Python method that, by default, sorts the list in ascending order. However, you can modify the order from ascending to descending by specifying the sorting criteria.

Example

Let's say you want to sort the element in prices in ascending order. You would type prices followed by a . [period] followed by the method name, i.e., sort including the parentheses.

prices = [238.11, 237.81, 238.91] prices.sort[] print[prices] [237.81, 238.11, 238.91]

type[] function

For the type[] function, it returns the class type of an object.

Example

Here we will see what type of both fam and fam2 are:

fam = ["liz", 1.73, "emma", 1.68, "mom", 1.71, "dad", 1.89] fam ['liz', 1.73, 'emma', 1.68, 'mom', 1.71, 'dad', 1.89]

Let's see what the type of the object is:

type[fam] list

Now, let's look at fam2.

fam2 = [["liz", 1.73], ["emma", 1.68], ["mom", 1.71], ["dad", 1.89]] fam2 [['liz', 1.73], ['emma', 1.68], ['mom', 1.71], ['dad', 1.89]]

Let's see what the type of the object is:

type[fam2] list

These calls show that both fam and fam2 are in fact lists.

append[] method

The append[] method will add certain content you enter to the end of the elements you select.

Example

In this example, let’s extend the string by adding “April” to the list with the method append[]. Using append[] will increase the length of the list by 1.

months = ['January', 'February', 'March'] months.append['April'] print[months]

When you run the above code, it produces the following result:

['January', 'February', 'March', 'April']

Video liên quan

Bài Viết Liên Quan

Bài mới nhất

Chủ Đề