8

Trying to add a count int to the end of a string (website url):

Code:

  count = 0
  while count < 20:
    Url = "http://www.ihiphopmusic.com/music/page/" 
    Url = (Url) + (count)
    #Url = Url.append(count)
    print Url

I want:

http://www.ihiphopmusic.com/music/page/2
http://www.ihiphopmusic.com/music/page/3
http://www.ihiphopmusic.com/music/page/4
http://www.ihiphopmusic.com/music/page/5

Results:

Traceback (most recent call last):
  File "grub.py", line 7, in <module>
    Url = Url + (count)
TypeError: cannot concatenate 'str' and 'int' objects
Santosh Kumar
  • 26,475
  • 20
  • 67
  • 118
jokajinx
  • 133
  • 1
  • 1
  • 7
  • Dude, use `url = ` instead of doing concatenation because in some forms of python (like Jython) you will incur massive overheads for string concats. – Snakes and Coffee Aug 17 '12 at 03:07

7 Answers7

20

The problem is exactly what the traceback states. Python doesn't know what to do with "hello" + 12345

You'll have to convert the integer count into a string first.

Additionally, you never increment the count variable, so your while loop will go on forever.

Try something like this:

count = 0
url = "http://example.com/"
while count < 20:
    print(url + str(count))
    count += 1

Or even better:

url = "http://example.com/"
for count in range(1, 21):
    print(url + str(count))

As Just_another_dunce pointed out, in Python 2.x, you can also do

print url + str(count)
Joel Cornett
  • 24,192
  • 9
  • 66
  • 88
5

Try

 Url = (Url) + str(count)

instead. The problem was that you were trying to concatenate a string and a number, rather than two strings. str() will fix this for you.

str() will provide a string version of count suitable for concatenation, without actually converting count to a string from an int. See this example:

>>> n = 55

>>> str(n)
>>> '55'

>>> n
>>> 55

Lastly, it is considered more efficient to format a string, rather than concatenate it. I.e.,

 Url = '%s%d' % (Url, count)

or

 Url = '{}{}'.format(Url, count)

Also, you have an infinite loop since the value of count is never changed inside the loop. To fix this add

count += 1

at the bottom of your loop.

Levon
  • 138,105
  • 33
  • 200
  • 191
3

Try casting count to a string as in

Url = "http://www.ihiphopmusic.com/music/page/" + str(count)

or use formatting

Url = "http://www.ihiphopmusic.com/music/page/%s" % count

or maybe even

Url = "http://www.ihiphopmusic.com/music/page/{count}".format(count=count) 
Snakes and Coffee
  • 8,747
  • 4
  • 40
  • 60
2

Use this:

url = "http://www.ihiphopmusic.com/music/page/" while count < 20: '''You can redefine the variable Also, you have to convert count to a string as url is also a string''' url = url + str(count) print url

1
Url = "http://www.ihiphopmusic.com/music/page/%d" % (count,)
Ignacio Vazquez-Abrams
  • 776,304
  • 153
  • 1,341
  • 1,358
0

You have to change the int to a string.

Url = (Url) + str(count)
Phil
  • 6,686
  • 2
  • 19
  • 25
0

You need to convert the integer to a string

count = 0
while count < 20:
    Url = "http://www.ihiphopmusic.com/music/page/"
    Url = (Url) + str(count)     
    #Url = Url.append(count)     
    print Url 
Joel Cornett
  • 24,192
  • 9
  • 66
  • 88
Adrian
  • 2,825
  • 1
  • 22
  • 32