By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,768 Members | 2,054 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,768 IT Pros & Developers. It's quick & easy.

Simple question about double quotes

P: 7
Hello,

I want to add quotes to the front and of strings. That is, I want to turn a list of strings like

Strawberry
Chocolate
Vanilla

into the following list:

"Strawberry"
"Chocolate"
"Vanilla"

I have noticed that a command like
string.replace(x,x[0],"\"\""+x[0])
will put two double quotes in front of a string, but I just want one double quote in front (and one in back).

How do I do this?
Aug 3 '07 #1
Share this Question
Share on Google+
2 Replies


Expert 100+
P: 511
Hello,

I want to add quotes to the front and of strings. That is, I want to turn a list of strings like

Strawberry
Chocolate
Vanilla

into the following list:

"Strawberry"
"Chocolate"
"Vanilla"

I have noticed that a command like
string.replace(x,x[0],"\"\""+x[0])
will put two double quotes in front of a string, but I just want one double quote in front (and one in back).

How do I do this?
there are many ways, one of which is:
Expand|Select|Wrap|Line Numbers
  1. >>> s="strawberry"
  2. >>> s="%s%s%s" % ('"',s,'"')
  3. >>> print s
  4. "strawberry"
  5. >>>
  6.  
  7.  
Aug 3 '07 #2

bartonc
Expert 5K+
P: 6,596
Hello,

I want to add quotes to the front and of strings. That is, I want to turn a list of strings like

Strawberry
Chocolate
Vanilla

into the following list:

"Strawberry"
"Chocolate"
"Vanilla"

I have noticed that a command like
string.replace(x,x[0],"\"\""+x[0])
will put two double quotes in front of a string, but I just want one double quote in front (and one in back).

How do I do this?
You can use double quotes inside single quotes, and vice versa, as if they are any other character:
Expand|Select|Wrap|Line Numbers
  1. >>> print '"strawberry"'
  2. "strawberry"
  3. >>> print "'strawberry'"
  4. 'strawberry'
  5. >>>
  6. >>> fruit = 'strawberry'
  7. >>> print "'%s'" %fruit
  8. 'strawberry'
  9. >>> print '"%s"' %fruit
  10. "strawberry"
  11. >>>
Aug 3 '07 #3

Post your reply

Sign in to post your reply or Sign up for a free account.