-1

i want the string 'abc'

abc="['one']"\n['two']"

to output

one 
two 

as far as i know:

.strip() only replaces characters at the beginning and at the ending of the string

.translate() only takes one variable

.replace() only works with 1 character.

Kevin
  • 74,910
  • 12
  • 133
  • 166
Michael
  • 65
  • 4
  • 2
    ".replace() only works with 1 character." Nope, it works with strings with any number of characters. `x.replace("hello I am a very long string", "yes I am also very long as well")` is just as valid as `x.replace("X", "Y")` – Kevin Dec 15 '15 at 20:36
  • 1
    yes.I wanted to say it can only replace 1 thing to another be it a string or a character – Michael Dec 15 '15 at 20:38
  • You really need to clarify exactly which characters you're trying to replace - are you wanting to keep all alphas and carriage returns, or are you wanting to remove all brackets and quotes, or are your requirements something else entirely? – Simon MᶜKenzie Dec 15 '15 at 23:19
  • 1
    I don't understand what `abc` actually contains; `abc="['one']"\n['two']"` is a syntax error. – Kyle Strand Dec 15 '15 at 23:28

2 Answers2

2

You can use re.

import re
abc = re.sub(r"['\[\]]", '', abc)

worked for me.

Jenny L
  • 111
  • 5
0

i sort of fixed the problem by

abcd=abc.replace("['","")
abcdf=abcd.replace("']","")

not a perfect solution but it works

Michael
  • 65
  • 4
  • You can of course omit the intermediate `abdc` by using `abc.replace("['","").replace("']","")`, but the other answer using `re.sub` is nicer anyway. – Simon MᶜKenzie Dec 15 '15 at 23:18