{"id":24829,"date":"2021-11-02T09:54:23","date_gmt":"2021-11-02T04:24:23","guid":{"rendered":"https:\/\/python-programs.com\/?p=24829"},"modified":"2021-11-05T20:37:44","modified_gmt":"2021-11-05T15:07:44","slug":"python-len-function-with-examples","status":"publish","type":"post","link":"https:\/\/python-programs.com\/python-len-function-with-examples\/","title":{"rendered":"Python len() Function with Examples"},"content":{"rendered":"
In the previous article, we have discussed Python locals() Function with Examples<\/a> The number of items in an object is returned by the len() function.<\/p>\n The len() function returns the number of characters in a string when the object is a string.<\/p>\n Syntax:<\/strong><\/p>\n Parameters<\/strong><\/p>\n object:<\/strong> This is Required. It is an object. It should be a sequence or a collection.<\/p>\n Return Value:<\/strong><\/p>\n The number of items in an object is returned by the len() function.<\/p>\n A TypeError exception will be thrown if an argument is not passed or if an invalid argument is passed.<\/p>\n Examples:<\/strong><\/p>\n Example1:<\/strong><\/p>\n Input:<\/strong><\/p>\n Output:<\/strong><\/p>\n Example2:<\/strong><\/p>\n Input:<\/strong><\/p>\n Output:<\/strong><\/p>\n Approach:<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n Approach:<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n Output:<\/strong><\/p>\n Approach:<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n Approach:<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n Fed up with searching various pages for the list of Python Built in Functions<\/a>? Look at the tutorial linked here and explore all coding samples of built-in functions of python.<\/p>\n 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 …<\/p>\n
\nlen() Function in Python:<\/strong><\/p>\nlen(object)<\/pre>\n
Given List = [1, 20, 3, 40, 5]<\/pre>\n
The length of the given list = 5<\/pre>\n
Given String = \"hello btechgeeks\"<\/pre>\n
The length of the given string = 16<\/pre>\n
len() Function with Examples in Python<\/h2>\n
\n
Method #1: Using Built-in Functions (Static Input)<\/h3>\n
1)For Lists<\/h5>\n
\n
# Give the list as static input and store it in a variable.\r\ngvn_lst = [1, 20, 3, 40, 5]\r\n# Pass the given list as an argument to the len() function to get the length\r\n# of the given list.\r\n# Store it in another variable.\r\nlst_lengt = len(gvn_lst)\r\n# Print the length of the given list.\r\nprint(\"The length of the given list = \", lst_lengt)\r\n<\/pre>\n
The length of the given list = 5<\/pre>\n
2)For Strings<\/h5>\n
\n
# Give the string as static input and store it in a variable.\r\ngvn_str = \"hello btechgeeks\"\r\n# Pass the given string as an argument to the len() function to get the length\r\n# of the given string.\r\n# Store it in another variable.\r\nstr_lengt = len(gvn_str)\r\n# Print the length of the given string.\r\nprint(\"The length of the given string = \", str_lengt)\r\n<\/pre>\n
The length of the given string = 16<\/pre>\n
How does len() work with dictionaries and sets?<\/h5>\n
# set doesn't allows duplicates\r\ngvn_set = {10, 20, 40, 50, 10}\r\nprint(\"The length of the given set\", gvn_set, \"=\", len(gvn_set))\r\n\r\ngvn_set2 = set()\r\nprint(\"The length of the given set\", gvn_set2, \"=\", len(gvn_set2))\r\n\r\ngvn_dictnry = {100: 'hello', 200: 'btechgeeks'}\r\nprint(\"The length of the given dictionary\", gvn_dictnry, \"=\", len(gvn_dictnry))\r\n<\/pre>\n
The length of the given set {40, 10, 20, 50} = 4\r\nThe length of the given set set() = 0\r\nThe length of the given dictionary {100: 'hello', 200: 'btechgeeks'} = 2<\/pre>\n
Method #2: Using Built-in Functions (User Input)<\/h3>\n
1)For Lists<\/h5>\n
\n
# Give the list as user input using list(),map(),input(),and split() functions.\r\n# Store it in a variable.\r\ngvn_lst = list(map(int, input(\r\n 'Enter some random List Elements separated by spaces = ').split()))\r\n# Pass the given list as an argument to the len() function to get the length\r\n# of the given list.\r\n# Store it in another variable.\r\nlst_lengt = len(gvn_lst)\r\n# Print the length of the given list.\r\nprint(\"The length of the given list = \", lst_lengt)\r\n<\/pre>\n
Enter some random List Elements separated by spaces = -1 -2 7 8 9 0 3\r\nThe length of the given list = 7<\/pre>\n
2)For Strings<\/h5>\n
\n
# Give the string as user input using the input() function and store it in a variable.\r\ngvn_str = input(\"Enter some random string = \")\r\n# Pass the given string as an argument to the len() function to get the length\r\n# of the given string.\r\n# Store it in another variable.\r\nstr_lengt = len(gvn_str)\r\n# Print the length of the given string.\r\nprint(\"The length of the given string = \", str_lengt)\r\n<\/pre>\n
Enter some random string = good morning btechgeeks\r\nThe length of the given string = 23<\/pre>\n
\n