Table of Contents
This guide covers wool ()() function in python with practical context and easy-to-follow details. Use it to understand the subject and apply the information confidently.
Wool ()() Function Syntax in Python
len(chuoi)
Wool Function Parameters ()()
Wool () has 1 parameter:
- String : String (can be string, bytes, tuple, list, range, dictionary, set or frozen set) that you want to calculate the length.
Value Returned from Wool ()()
- The len function returns the number of characters of the input string. If the parameter or parameter is not passed, the program generates a TypeError exception.
Example 1: How Does the Function Len ()() Work with the Parameters of
testList = []print(testList, 'co do dai la', len(testList))testList = [1, 2, 3]print(testList, 'co do dai la', len(testList))testTuple = (1, 2, 3)print(testTuple, 'co do dai la', len(testTuple))testRange = range(1, 10)print('Do dai cua', testRange, 'la', len(testRange))
Run the program, the result is returned
[] co do dai la 0[1, 2, 3] co do dai la 3(1, 2, 3) co do dai la 3Do dai cua range(1, 10) la 9
Example 2: How Does the Len ()() Function Work with Parameters That Are
testString = ''print('Do dai cua', testString, 'la', len(testString))testString = 'Tipsmake'print('Do dai cua', testString, 'la', len(testString))# byte objecttestByte = b'Tipsmake'print('Do dai cua', testByte, 'la', len(testByte))testList = [1, 2, 3]# converting to bytes objecttestByte = bytes(testList)print('Do dai cua', testByte, 'la', len(testByte))
Running the program we get the result is:
Do dai cua la 0Do dai cua Tipsmake la 11Do dai cua b'Tipsmake' la 11Do dai cua b'x01x02x03' la 3
Example 3: How Does the Len ()() Function Work with Parameters That Are
testSet = {1, 2, 3}print(testSet, 'co do dai la', len(testSet))# Empty SettestSet = set()print(testSet, 'co do dai la', len(testSet))testDict = {1: 'mot', 2: 'hai'}print(testDict, 'co do dai la', len(testDict))testDict = {}print(testDict, 'co do dai la', len(testDict))testSet = {1, 2}# frozenSetfrozenTestSet = frozenset(testSet)print(frozenTestSet, 'co do dai la', len(frozenTestSet))
The result returned is:
{1, 2, 3} co do dai la 3set() co do dai la 0{1: 'mot', 2: 'hai'} co do dai la 2{} co do dai la 0frozenset({1, 2}) co do dai la 2
When executed, len () calls the __len__ method of the object, so you can also imagine the len () function as:
def len(s):return s.__len__()
Therefore, you can assign custom lengths to objects (if needed).
Example 4: How Does the Len ()() Function Work with Custom Objects?
class Session:def __init__(self, number = 0):self.number = numberdef __len__(self):return self.number# ?? dài m?c ??nh là 0# vi?t b?i TipsMake.coms1 = Session()print(len(s1))# gán chi?u dài tùy ch?nhs2 = Session(6)print(len(s2))
Run the program, the returned result is:
06
Previous lesson: int () function in Python
Next lesson: function getattr () in Python
FAQ
What should I check before following these steps?
Confirm device and software compatibility, save important data, and make sure you have the required permissions, files, and account access.
Why might the process not work?
Common causes include outdated software, missing permissions, incompatible hardware, an unstable connection, or completing a step in the wrong order.
Can I undo the changes if necessary?
That depends on the tool or setting. Use built-in restore options when available, keep a backup, and record the original configuration first.
Reader Comments 0
Sign in with email or Google to join the discussion.