1 Replies - 51440 Views - Last Post: 01 July 2010 - 10:54 AM Rate Topic: -----

#1 brianmen   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 39
  • Joined: 14-June 09

Issues removing quotes from a string

Posted 01 July 2010 - 10:48 AM

Hi everyone... I'm trying to make a function that will remove quotes from a string so that that sting can become a dictionary key that I can call on later. I've tried a few methods to remove the quotes, but it does not seem to work. If you run this code, you'll see what I mean... Oh and I do mean to leave that uppercase part in the function.

def cellparser(cell):
    for character in cell:
        if (character=='\"'):
        if (character=="\'"):
    for character in cell:
        if (character=="\'") or (character=='\"'):
    return cell

string=' "HELLO /" regarding those \' and \" \' quotes. They are annoying." '
print "string before modification is"
print string

print "string parsed immediately"
print cellparser(string)

print "string redefined"
print string

My output is still showing quotes in the center where I put those slashes....

Is This A Good Question/Topic? 0
  • +

Replies To: Issues removing quotes from a string

#2 Motoma   User is offline

  • D.I.C Addict
  • member icon

Reputation: 452
  • View blog
  • Posts: 798
  • Joined: 08-June 10

Re: Issues removing quotes from a string

Posted 01 July 2010 - 10:54 AM

Hello there.

What you are looking for is the replace() method:

string = string.replace('"', '')
string = string.replace("'", "")

or more concisely:

string = string.replace('"', '').replace("'", '')

Additionally, dictionaries can have quotes in keys!

>>> d['"'] = 'a'
>>> d
{'"': 'a'}

This post has been edited by Motoma: 01 July 2010 - 10:59 AM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1