So, quick question, (has to be just something I'm missing), if I want to concantate a string and integers in a url, is there a way to do so? I'm sure there is...I mean this IS python!

from urllib2 import urlopen 

width = input("Please enter a width specification here: ")
height = input("Please enter a height specification here: ")

url = "http://www.placekitten.com/" + width + "/" + height 
kitten = urlopen(url).read() 

kitten_file = open('/Users/SirPrinceKai/Desktop/adorable_kitten.jpg', 'w') 
kitten_file.write(kitten) 
kitten_file.close() 

Recommended Answers

All 2 Replies

urlopen() take string as argument.
So no need to use input() that return integer,use raw_input().
You should by the way never use input() in python 2.x,use int(raw_input()) for integer return.
input() in python 2.x has some safty issues,this has been fixed in python 3.

String formatting is natural way to do this in python.

>>> width = 640
>>> height = 480
>>> "http://www.placekitten.com/%s/%s" % (width,height)
'http://www.placekitten.com/640/48

New string foramtting,this is the preferred way

>>> width = 640
>>> height = 480
>>> "http://www.placekitten.com/{}/{}".format(width,height)
'http://www.placekitten.com/640/480'
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.