BASE_URL = 'http://foobar.com?foo=%s'
variable = 'bar'
final_url = BASE_URL % (variable)
I get this 'http://foobar.com?foo=bar' # It ignores the inside string.
But i wanted something like this 'http://foobar.com?foo='bar''
Thanks for the answer.
Can you help me out with almost the same problem:
lst = ['foo', 'bar', 'foo bar']
[str(l) for l in lst if ' ' in l]
I get ['foo bar'] but i wanted it like [''foo bar'']
Thanks in advance.
-
Why do you absolutely want the quotes in there in the first place?Horst Gutmann– Horst Gutmann2009年04月06日 11:21:05 +00:00Commented Apr 6, 2009 at 11:21
-
Actually, i am trying to use twitter search api to get the search result related to my keywords. But when there is a space inside a search string it asks it as foobar.com?q=foo+OR+bar+OR+'foo bar'aatifh– aatifh2009年04月06日 11:27:11 +00:00Commented Apr 6, 2009 at 11:27
-
Ahh OK :-) Please always also write why you want to get a certain behaviour out of your code. This makes it easier to grasp your whole problem :-)Horst Gutmann– Horst Gutmann2009年04月06日 11:31:09 +00:00Commented Apr 6, 2009 at 11:31
-
... hit the button too soon: For this particular problem, simply add your quotes to the string but then still let urlencode run over it since there is no " " (space) character in URLs :-)Horst Gutmann– Horst Gutmann2009年04月06日 11:34:04 +00:00Commented Apr 6, 2009 at 11:34
4 Answers 4
If you're working with URL parameters, it's probably safer to use urllib.urlencode:
import urllib
BASE_URL = 'http://foobar.com/?%s'
print BASE_URL % urllib.urlencode({
'foo': 'bar',
})
Regarding the quotes: Why do you explicitly want them? Normally your HTTP-wrapper would handle all that for you.
Regarding your 2nd question: If you absolutely also want to have the quotes in there, you still have to either escape them when appending the contained string, or (probably the safer way of doing it) would be using repr(...)
lst = ['foo', 'bar', 'foo bar']
lst2 = []
for l in lst:
if ' ' in l:
lst2.append(repr(l))
Comments
Change your BASE_URL to either
BASE_URL = "http://foobar.com?foo='%s'"
or
BASE_URL = 'http://foobar.com?foo=\'%s\''
Comments
It seems, you are a bit confused about how string literals work.
When you say s = 'this is a string', you are assigning a string to a variable. What string? Well, a string literal that you hardcoded in your program.
Python uses the apostrophes to indicate start and end of a string literal - with anything inside being the contents of the string.
This is probably one of the first hard problems for beginners in programming: There is a difference between what you write in your programs source code and what actually happens at runtime. You might want to work through a couple of tutorials (I hear "Dive into Python" is pretty good).
Comments
If you want single quotes to appear in URL you can use
BASE_URL = 'http://foobar.com?foo=%s'
variable = "'bar'"
final_url = BASE_URL % (variable)
But this variant is quite insecure, if variable is coming from somewhere (like user input).