Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Python Python Basics Functions and Looping Functions

I took so many courses but I always winder Why do I need to use "len" as a programmer?

I took so many courses but I always winder Why do I need to use "len" as a programmer?

2 Answers

mouseandweb
mouseandweb
13,758 Points

I have another one.

Say you download a huge load of information in the form of a string. Could be lists, an extremely long address-book, DNA sequence, etc. You downloaded this never-ending block of data and you just want to print them into a text document with pagination. You can len(wall_of_text) to see how long it is in its entirety. You can also split the text using len() as a guide. About 3000 characters can fit on one page, so you can take the len(wall_of_text) and divide it by 3000 to see how many pages of info you downloaded. Can also use this to separate the info into pages with page numbers (pagination)... and then maybe print it onto pages in a website or even print on paper!

mouseandweb
mouseandweb
13,758 Points

An easy example is checking the length of input when a user creates a password. If passwords need to be a certain length, or at least more than a minimum length, you could use the len() function.

Sometimes that function would be abstracted away where it is in use but you never actually type it.