pythonnoob
pythonnoob

Reputation: 19

Removing the single quotes after using re.sub() in python

After replacing all word characters in a string with the character '^', using re.sub("\w", "^" , stringorphrase) I'm left with :

>>> '^^^ ^^ ^^^^'

Is there any way to remove the single quotes so it looks cleaner?

>>> ^^^ ^^ ^^^^

Upvotes: 0

Views: 3099

Answers (3)

Jon Clements
Jon Clements

Reputation: 142136

Are you sure it's just not how it's displayed in the interactive prompt or something (and there aren't actually apost's in your string)?

If the ' is actually part of the string, and is first/last then either:

string = string.strip("'")

or:

string = string[1:-1] # lop ending characters off

Upvotes: 6

Mark Ransom
Mark Ransom

Reputation: 308138

Use the print statement. The quotes aren't actually part of the string.

Upvotes: 4

inspectorG4dget
inspectorG4dget

Reputation: 113945

To remove all occurrences of single quotes:

mystr = some_string_with_single_quotes
answer = mystr.replace("'", '')

To remove single quotes ONLY at the ends of the string:

mystr = some_string_with_single_quotes
answer = mystr.strip("'")

Hope this helps

Upvotes: 0

Related Questions