A little knowledge that must be mastered--python Len example

Source: Internet
Author: User
Description

The Python len () method returns the length of an object (character, list, tuple, and so on) or the number of items.

Grammar

Len () method syntax:

Len (s)

return value

Returns the length of the object.

Instance

The following examples show how Len () is used:

>>>str = "Runoob" >>> len (str)             # string length 6>>> L = [1,2,3,4,5]>>> len (l)               # Number of list elements 5

Len is called when it calls Len (instance). Len is a built-in function that can return the length of an object. It can be used for any object that is supposed to have a length.

The Len of the string is the number of its characters, and the Len of the dictionary is the number of its keywords, and the Len of the list or sequence is the number of elements. For class instances, define the Len method, and then self-edit ...

In SQLite3, to query the number of results of a SELECT statement, you cannot directly use the Len (RES) statement, you need to assign the cursor's Fetchall method to an array, and then use the Len method for the data, as follows:

   Import sqlite3        conn = sqlite.connect (' test.db ')    cursor = conn.cursor ()    cursor.execute ("Select UID, Username, userpwd from member ")    records = Cursor.fetchall ()    num = Len (records)

"Recommended"

1. Learn more about the special function __len__ (self) in Python

2. Summarize the use instances of the Len () function in Python

3. Example Tutorials for using Python-specific class methods

4. Python Magic method __getitem__, __setitem__, __delitem__, __len__ respectively introduced

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.