2
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.

asked Apr 6, 2009 at 10:43
4
  • Why do you absolutely want the quotes in there in the first place? Commented 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' Commented 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 :-) Commented 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 :-) Commented Apr 6, 2009 at 11:34

4 Answers 4

7

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))
answered Apr 6, 2009 at 10:50
Sign up to request clarification or add additional context in comments.

Comments

7

Change your BASE_URL to either

BASE_URL = "http://foobar.com?foo='%s'"

or

BASE_URL = 'http://foobar.com?foo=\'%s\''
answered Apr 6, 2009 at 10:45

Comments

3

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).

answered Apr 6, 2009 at 11:27

Comments

1

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).

answered Apr 7, 2009 at 8:52

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.