By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,675 Members | 2,255 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,675 IT Pros & Developers. It's quick & easy.

integer from argument

P: 12
Hello, I just started learning Python today. I already know Perl, and they have a veery similar syntax, so it's not really hard. But I cannot manage to get an integer from an argument.
If i use sys.argv, it only gets it as a "string". But I need it as an integer.
If I try to use it in a calculation, I get:
TypeError: cannot concatenate 'str' and 'int' objects

Help?

Thanks
May 9 '07 #1
Share this Question
Share on Google+
2 Replies


bvdet
Expert Mod 2.5K+
P: 2,851
Hello, I just started learning Python today. I already know Perl, and they have a veery similar syntax, so it's not really hard. But I cannot manage to get an integer from an argument.
If i use sys.argv, it only gets it as a "string". But I need it as an integer.
If I try to use it in a calculation, I get:
TypeError: cannot concatenate 'str' and 'int' objects

Help?

Thanks
Expand|Select|Wrap|Line Numbers
  1. >>> s = '1234'
  2. >>> type(s)
  3. <type 'str'>
  4. >>> i = int(s)
  5. >>> type(i)
  6. <type 'int'>
  7. >>> 
Sometimes the most obvious is the hardest to see.
May 9 '07 #2

P: 12
Thanks for your reply.
I started looking into it, and by some testing, I saw that python uses the filename as sys.argv[0](perl doesn't do this, so I wasn't expecting that), so that's the reason a = int(s) didn't work. I used sys.argv[1] and everything is working perfectly.

Thanks a lot :)
May 9 '07 #3

Post your reply

Sign in to post your reply or Sign up for a free account.