Welcome to the Treehouse Community
The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

Bill McKenna
586 PointsPrinting String X amount of times.
When we use the code print(input_string*int(input_int)) before running it I would have assumed we would get and error because we are trying to multiply a string times an integer. How does python know that we are just looking to print it x amount of times. x being Int(input_int)
2 Answers

Dan Johnson
40,532 PointsThe str object in Python defines the __mul__
method which which effectively overloads the multiplication operator. Defining __mul__
lets you do stuff like this:
class Message:
def __init__(self, message):
self.message = message
def __mul__(self, operand):
repeated_message = ""
for i in range(1, operand+1):
repeated_message += str(i) + ": " + self.message
return repeated_message
msg = Message("Overloading the multiplication operator.\n")
print(msg * 5)
Will display:
1: Overloading the multiplication operator.
2: Overloading the multiplication operator.
3: Overloading the multiplication operator.
4: Overloading the multiplication operator.
5: Overloading the multiplication operator.
Here's a list of special method names from the documentation.

Iain Simmons
Treehouse Moderator 32,252 PointsFollowing on from Dan Johnson 's excellent info, if you run Python in a console/terminal and enter the following command:
help(str.__mul__)
...you'll see how the str
class/object implements the __mul__
method.
Try the following command to see a list of other built-in methods that the str
object has:
dir(str)
Bill McKenna
586 PointsBill McKenna
586 PointsThanks Dan for the help