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

In Python Basics, Raise an Exception. How can I make sure answer is more than 3 characters?

As part of the exercise we are asked to create code to raise an exception if the provided answer is less than 3 characters. How do you quantify characters in Python, and how should that be coded? THANKS!

1 Answer

The len() function will return the number of characters in a string. For example:

x = len("Hello")

x is 5.

You will want to use an if statement, comparing the length of the given string to 3, and if it is less than 3 raise the error.

Thanks!!