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.

Python Python Basics Types and Branching Strings and Operators

Regarding "i" *20 why its works and "i" +20 not equal to i20 and error ?

Regarding

"i" *20 why its works and "i" +20 not equal to i20 and error ?

2 Answers

Python can not concatenate a string and an integer. They are considered two separate types of object. "i"*20 concatenates strings (20 i's). "i" + 20 attempts to concatenate a string and an integer. To get that to work you would have to convert 20 to a string "i" + str(20).

THANKS

marin stefan daniel
marin stefan daniel
3,186 Points

"i" * 20 is multiplying the string "i" 20 times "i" + 20 doesn't work because "i" is a string and 20 is a int .. you can not concatenate a string with a int nor add a int (number) with a sting . you can "i" + "20" so it will print "i20" ..meaning the 20 ..must be with "" so it will become a string as well