Simple question about double quotes

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • mh121
    New Member
    • Aug 2007
    • 7

    #1

    Simple question about double quotes

    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?
  • ghostdog74
    Recognized Expert Contributor
    • Apr 2006
    • 511

    #2
    Originally posted by mh121
    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:
    Code:
    >>> s="strawberry"
    >>> s="%s%s%s" % ('"',s,'"')
    >>> print s
    "strawberry"
    >>>

    Comment

    • bartonc
      Recognized Expert Expert
      • Sep 2006
      • 6478

      #3
      Originally posted by mh121
      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:[CODE=python]
      >>> print '"strawberry "'
      "strawberry "
      >>> print "'strawberr y'"
      'strawberry'
      >>>
      >>> fruit = 'strawberry'
      >>> print "'%s'" %fruit
      'strawberry'
      >>> print '"%s"' %fruit
      "strawberry "
      >>>[/CODE]

      Comment

      Working...