20

I need a string consisting of a repetition of a particular character. At the Python console, if I type:

n = '0'*8

then n gets assigned a string consisting of 8 zeroes, which is what I expect.

But, if I have the same in a Python program (.py file), then the program aborts with an error saying
can't multiply sequence by non-int of type 'str'

Any way to fix this?

Georgy
  • 12,464
  • 7
  • 65
  • 73
Paul
  • 209
  • 1
  • 2
  • 3
  • 1
    well the error message means that you're both multiplication arguments are interpreted as a string. 8 doesn't become a string just by putting it into a file - there *must* be something else going on... care to show the file? – Nicolas78 Aug 21 '10 at 08:58
  • the mentioned error message is coming when i put it like n = '0'*'8' pls check your expression – Ankit Jaiswal Aug 21 '10 at 09:01
  • The way compilers and interpreters work means that some error messages will appear in weird locations, like two lines after or at the end of the line. It's best if we have some context in your program, because the issue could be scoping or something similar. **tl;dr** Give us more lines of code to work with – Lightfire228 Apr 25 '16 at 16:04

5 Answers5

39

You get that error because - in your program - the 8 is actually a string, too.

>>> '0'*8
'00000000'
>>> '0'*'8' # note the ' around 8
(I spare you the traceback)
TypeError: can't multiply sequence by non-int of type 'str'
9

I could bet you're using raw_input() to read the value which multiplies the string. You should use input() instead to read the value as an integer, not a string.

kirbuchi
  • 2,274
  • 2
  • 23
  • 35
  • Didn't realize this -- thanks!. For other noobs, it's for `python2` ([see this](https://stackoverflow.com/questions/4915361/whats-the-difference-between-raw-input-and-input-in-python3-x)). – Hendy May 22 '17 at 22:32
2

The reason that you are getting the error message is that you're trying to use multiplying operator on non integer value.

The simplest thing that will do the job is this:

>>> n = ''.join(['0' for s in xrange(8)])
>>> n
'00000000'
>>>

Or do the function for that:

>>> def multiply_anything(sth, size):
...     return ''.join(["%s" % sth for s in xrange(size)])
...
>>> multiply_anything(0,8)
'00000000'
>>>
jabbas
  • 57
  • 3
1

If you want the result to be a list of strings instead of a single one, you can always use this:

list(('0',) * 8)

And you can get:

['0', '0', '0', '0', '0', '0', '0', '0']
LAS
  • 21
  • 1
0

That line of code works fine from a .py executed here, using python 2.6.5, you must be executing the script with a different python version.

João Pinto
  • 5,521
  • 5
  • 21
  • 35
  • 1
    This is nothing to do with the python version - it is a bug in his progam, as delnan says. – Dave Kirby Aug 21 '10 at 08:58
  • 1
    Actually there was an error on the question information on the first place, according to the question he was using '0'*8 which is valid, assuming that information was correct a probably answer was that he was using an 'invalid' python interpreter. – João Pinto Aug 21 '10 at 22:31