Python len() Function with Examples

In the previous article, we have discussed Python locals() Function with Examples
len() Function in Python:

The number of items in an object is returned by the len() function.

The len() function returns the number of characters in a string when the object is a string.

Syntax:

len(object)

Parameters

object: This is Required. It is an object. It should be a sequence or a collection.

Return Value:

The number of items in an object is returned by the len() function.

A TypeError exception will be thrown if an argument is not passed or if an invalid argument is passed.

Examples:

Example1:

Input:

Given List = [1, 20, 3, 40, 5]

Output:

The length of the given list =  5

Example2:

Input:

Given String = "hello btechgeeks"

Output:

The length of the given string =  16

len() Function with Examples in Python

Method #1: Using Built-in Functions (Static Input)

1)For Lists

Approach:

  • Give the list as static input and store it in a variable.
  • Pass the given list as an argument to the len() function to get the length of the given list.
  • Store it in another variable.
  • Print the length of the given list.
  • The Exit of the program.

Below is the implementation:

# Give the list as static input and store it in a variable.
gvn_lst = [1, 20, 3, 40, 5]
# Pass the given list as an argument to the len() function to get the length
# of the given list.
# Store it in another variable.
lst_lengt = len(gvn_lst)
# Print the length of the given list.
print("The length of the given list = ", lst_lengt)

Output:

The length of the given list =  5
2)For Strings

Approach:

  • Give the string as static input and store it in a variable.
  • Pass the given string as an argument to the len() function to get the length of the given string.
  • Store it in another variable.
  • Print the length of the given string.
  • The Exit of the program.

Below is the implementation:

# Give the string as static input and store it in a variable.
gvn_str = "hello btechgeeks"
# Pass the given string as an argument to the len() function to get the length
# of the given string.
# Store it in another variable.
str_lengt = len(gvn_str)
# Print the length of the given string.
print("The length of the given string = ", str_lengt)

Output:

The length of the given string =  16
How does len() work with dictionaries and sets?
# set doesn't allows duplicates
gvn_set = {10, 20, 40, 50, 10}
print("The length of the given set", gvn_set, "=", len(gvn_set))

gvn_set2 = set()
print("The length of the given set", gvn_set2, "=", len(gvn_set2))

gvn_dictnry = {100: 'hello', 200: 'btechgeeks'}
print("The length of the given dictionary", gvn_dictnry, "=", len(gvn_dictnry))

Output:

The length of the given set {40, 10, 20, 50} = 4
The length of the given set set() = 0
The length of the given dictionary {100: 'hello', 200: 'btechgeeks'} = 2

Method #2: Using Built-in Functions (User Input)

1)For Lists

Approach:

  • Give the list as user input using list(),map(),input(),and split() functions.
  • Store it in a variable.
  • Pass the given list as an argument to the len() function to get the length of the given list.
  • Store it in another variable.
  • Print the length of the given list.
  • The Exit of the program.

Below is the implementation:

# Give the list as user input using list(),map(),input(),and split() functions.
# Store it in a variable.
gvn_lst = list(map(int, input(
   'Enter some random List Elements separated by spaces = ').split()))
# Pass the given list as an argument to the len() function to get the length
# of the given list.
# Store it in another variable.
lst_lengt = len(gvn_lst)
# Print the length of the given list.
print("The length of the given list = ", lst_lengt)

Output:

Enter some random List Elements separated by spaces = -1 -2 7 8 9 0 3
The length of the given list = 7
2)For Strings

Approach:

  • Give the string as user input using the input() function and store it in a variable.
  • Pass the given string as an argument to the len() function to get the length of the given string.
  • Store it in another variable.
  • Print the length of the given string.
  • The Exit of the program.

Below is the implementation:

# Give the string as user input using the input() function and store it in a variable.
gvn_str = input("Enter some random string = ")
# Pass the given string as an argument to the len() function to get the length
# of the given string.
# Store it in another variable.
str_lengt = len(gvn_str)
# Print the length of the given string.
print("The length of the given string = ", str_lengt)

Output:

Enter some random string = good morning btechgeeks
The length of the given string = 23

Fed up with searching various pages for the list of Python Built in Functions? Look at the tutorial linked here and explore all coding samples of built-in functions of python.