[Tutor] string integers?

Hugo Arts hugo.yoshi at gmail.com
Sun Feb 12 14:51:45 CET 2012

On Sun, Feb 12, 2012 at 2:25 PM, William Stewart <williamjstewart at rogers.com
> wrote:

> I am trying to get 2 string variables and 2 integer variables to be able
> to be multiplied
> can anyone tell me what I did wrong
> str1 = raw_input("Type in a String: ")
> str2 = raw_input("Type in a String: ")
> int1 = raw_input("Type in a integer variable: ")
> int2 = raw_input("Type in a integer variable: ")
> print str1 + str2 + int1 + int2
> import math
> print str1, "*", str2, "*", int1, "*"int2  "=", str1, * str2, * int1 * int
> 2
> and it wont let me write int2
> I know this may look stupid to most people  but I am just new at this so
> dont laugh  :)
No worries, this list is aimed toward new people. No one asking questions
here looks stupid.

Now, when you post to places like this, make sure you mention
1) what you thought would happen or what you're trying to do (you've got
this one decently covered)
2) what happened instead (if an error message happened, paste it!!! It's
almost always useful to us).

Your first problem is the commas in the print statement. You have commas in
places where they shouldn't be, and no commas in places where they should.
I'm not gonna give everything away, but look real close at it again. If you
have to put a lot of variables inside a string, all these commas do end up
being fairly confusing. You might want to have a look at the format method,
I'll give you a sample:

print "{0} * {1} * {2} * {3} = {4}".format(str1, str2, int1, int2, str1 *
str2 * int1 * int2)

The curly brackets are replaced by an argument of the format call. The
number indicates which argument. This way, you don't get commas all over
the place and you keep things nice and organized. You should also know that
you really can't multiply strings. You'll get a TypeError if you try.

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20120212/0797e988/attachment.html>

More information about the Tutor mailing list