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

len function

can someone explain in length when and how to use the "len function" thanks in advance

1 Answer

Jonathan Grieve
MOD
Jonathan Grieve
Treehouse Moderator 91,254 Points

Hi there.

You can call len() on a string to get the length (which is the number of characters) on that string. So you define a variable and then pass that variable to the len function.

string = "abcdefghi"
length = len(string)
print(length)

FYI it isn't conventional to use semicolons in Python ;)

why are you calling len length ?

Jonathan Grieve
Jonathan Grieve
Treehouse Moderator 91,254 Points

Because I'm using the code as an example of how you can use the length function to return and display the number of characters in a given string.

I'm now removing the semi colons from my example.