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
Shmuel Zilberman
1,409 Pointslen function
can someone explain in length when and how to use the "len function" thanks in advance
1 Answer
Jonathan Grieve
Treehouse Moderator 91,254 PointsHi 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)
Alexander Davison
65,469 PointsAlexander Davison
65,469 PointsFYI it isn't conventional to use semicolons in Python ;)
Jonathan Grieve
Treehouse Moderator 91,254 PointsJonathan Grieve
Treehouse Moderator 91,254 PointsQuite right. Force of habit. ???
Shmuel Zilberman
1,409 PointsShmuel Zilberman
1,409 Pointswhy are you calling len length ?
Jonathan Grieve
Treehouse Moderator 91,254 PointsJonathan Grieve
Treehouse Moderator 91,254 PointsBecause 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.