Removing a list of characters in string - python

I want to remove characters in a string in python:
string.replace(',', '').replace("!", '').replace(":", '').replace(";", '')...
But I have many characters I have to remove. I thought about a list
list = [',', '!', '.', ';'...]
But how can I use the list to replace the characters in the string?

If you're using python2 and your inputs are strings (not unicodes), the absolutely best method is str.translate:
>>> chars_to_remove = ['.', '!', '?']
>>> subj = 'A.B!C?'
>>> subj.translate(None, ''.join(chars_to_remove))
'ABC'
Otherwise, there are following options to consider:
A. Iterate the subject char by char, omit unwanted characters and join the resulting list:
>>> sc = set(chars_to_remove)
>>> ''.join([c for c in subj if c not in sc])
'ABC'
(Note that the generator version ''.join(c for c ...) will be less efficient).
B. Create a regular expression on the fly and re.sub with an empty string:
>>> import re
>>> rx = '[' + re.escape(''.join(chars_to_remove)) + ']'
>>> re.sub(rx, '', subj)
'ABC'
(re.escape ensures that characters like ^ or ] won't break the regular expression).
C. Use the mapping variant of translate:
>>> chars_to_remove = [u'δ', u'Γ', u'ж']
>>> subj = u'AжBδCΓ'
>>> dd = {ord(c):None for c in chars_to_remove}
>>> subj.translate(dd)
u'ABC'
Full testing code and timings:
#coding=utf8
import re
def remove_chars_iter(subj, chars):
sc = set(chars)
return ''.join([c for c in subj if c not in sc])
def remove_chars_re(subj, chars):
return re.sub('[' + re.escape(''.join(chars)) + ']', '', subj)
def remove_chars_re_unicode(subj, chars):
return re.sub(u'(?u)[' + re.escape(''.join(chars)) + ']', '', subj)
def remove_chars_translate_bytes(subj, chars):
return subj.translate(None, ''.join(chars))
def remove_chars_translate_unicode(subj, chars):
d = {ord(c):None for c in chars}
return subj.translate(d)
import timeit, sys
def profile(f):
assert f(subj, chars_to_remove) == test
t = timeit.timeit(lambda: f(subj, chars_to_remove), number=1000)
print ('{0:.3f} {1}'.format(t, f.__name__))
print (sys.version)
PYTHON2 = sys.version_info[0] == 2
print ('\n"plain" string:\n')
chars_to_remove = ['.', '!', '?']
subj = 'A.B!C?' * 1000
test = 'ABC' * 1000
profile(remove_chars_iter)
profile(remove_chars_re)
if PYTHON2:
profile(remove_chars_translate_bytes)
else:
profile(remove_chars_translate_unicode)
print ('\nunicode string:\n')
if PYTHON2:
chars_to_remove = [u'δ', u'Γ', u'ж']
subj = u'AжBδCΓ'
else:
chars_to_remove = ['δ', 'Γ', 'ж']
subj = 'AжBδCΓ'
subj = subj * 1000
test = 'ABC' * 1000
profile(remove_chars_iter)
if PYTHON2:
profile(remove_chars_re_unicode)
else:
profile(remove_chars_re)
profile(remove_chars_translate_unicode)
Results:
2.7.5 (default, Mar 9 2014, 22:15:05)
[GCC 4.2.1 Compatible Apple LLVM 5.0 (clang-500.0.68)]
"plain" string:
0.637 remove_chars_iter
0.649 remove_chars_re
0.010 remove_chars_translate_bytes
unicode string:
0.866 remove_chars_iter
0.680 remove_chars_re_unicode
1.373 remove_chars_translate_unicode
---
3.4.2 (v3.4.2:ab2c023a9432, Oct 5 2014, 20:42:22)
[GCC 4.2.1 (Apple Inc. build 5666) (dot 3)]
"plain" string:
0.512 remove_chars_iter
0.574 remove_chars_re
0.765 remove_chars_translate_unicode
unicode string:
0.817 remove_chars_iter
0.686 remove_chars_re
0.876 remove_chars_translate_unicode
(As a side note, the figure for remove_chars_translate_bytes might give us a clue why the industry was reluctant to adopt Unicode for such a long time).

You can use str.translate():
s.translate(None, ",!.;")
Example:
>>> s = "asjo,fdjk;djaso,oio!kod.kjods;dkps"
>>> s.translate(None, ",!.;")
'asjofdjkdjasooiokodkjodsdkps'

You can use the translate method.
s.translate(None, '!.;,')

If you are using python3 and looking for the translate solution - the function was changed and now takes 1 parameter instead of 2.
That parameter is a table (can be dictionary) where each key is the Unicode ordinal (int) of the character to find and the value is the replacement (can be either a Unicode ordinal or a string to map the key to).
Here is a usage example:
>>> list = [',', '!', '.', ';']
>>> s = "This is, my! str,ing."
>>> s.translate({ord(x): '' for x in list})
'This is my string'

''.join(c for c in myString if not c in badTokens)

Why not a simple loop?
for i in replace_list:
string = string.replace(i, '')
Also, avoid naming lists 'list'. It overrides the built-in function list.

Another approach using regex:
''.join(re.split(r'[.;!?,]', s))

you could use something like this
def replace_all(text, dic):
for i, j in dic.iteritems():
text = text.replace(i, j)
return text
This code is not my own and comes from here its a great article and dicusses in depth doing this

simple way,
import re
str = 'this is string ! >><< (foo---> bar) #-tuna-# sandwich-%-is-$-* good'
// condense multiple empty spaces into 1
str = ' '.join(str.split()
// replace empty space with dash
str = str.replace(" ","-")
// take out any char that matches regex
str = re.sub('[!##$%^&*()_+<>]', '', str)
output:
this-is-string--foo----bar--tuna---sandwich--is---good

Also an interesting topic on removal UTF-8 accent form a string converting char to their standard non-accentuated char:
What is the best way to remove accents in a python unicode string?
code extract from the topic:
import unicodedata
def remove_accents(input_str):
nkfd_form = unicodedata.normalize('NFKD', input_str)
return u"".join([c for c in nkfd_form if not unicodedata.combining(c)])

Perhaps a more modern and functional way to achieve what you wish:
>>> subj = 'A.B!C?'
>>> list = set([',', '!', '.', ';', '?'])
>>> filter(lambda x: x not in list, subj)
'ABC'
please note that for this particular purpose it's quite an overkill, but once you need more complex conditions, filter comes handy

Remove *%,&#! from below string:
s = "this is my string, and i will * remove * these ** %% "
new_string = s.translate(s.maketrans('','','*%,&#!'))
print(new_string)
# output: this is my string and i will remove these

How about this - a one liner.
reduce(lambda x,y : x.replace(y,"") ,[',', '!', '.', ';'],";Test , , !Stri!ng ..")

i think this is simple enough and will do!
list = [",",",","!",";",":"] #the list goes on.....
theString = "dlkaj;lkdjf'adklfaj;lsd'fa'dfj;alkdjf" #is an example string;
newString="" #the unwanted character free string
for i in range(len(TheString)):
if theString[i] in list:
newString += "" #concatenate an empty string.
else:
newString += theString[i]
this is one way to do it. But if you are tired of keeping a list of characters that you want to remove, you can actually do it by using the order number of the strings you iterate through. the order number is the ascii value of that character. the ascii number for 0 as a char is 48 and the ascii number for lower case z is 122 so:
theString = "lkdsjf;alkd8a'asdjf;lkaheoialkdjf;ad"
newString = ""
for i in range(len(theString)):
if ord(theString[i]) < 48 or ord(theString[i]) > 122: #ord() => ascii num.
newString += ""
else:
newString += theString[i]

In Python 3.8, this works for me:
s.translate(s.maketrans(dict.fromkeys(',!.;', '')))

These days I am diving into scheme, and now I think am good at recursing and eval. HAHAHA. Just share some new ways:
first ,eval it
print eval('string%s' % (''.join(['.replace("%s","")'%i for i in replace_list])))
second , recurse it
def repn(string,replace_list):
if replace_list==[]:
return string
else:
return repn(string.replace(replace_list.pop(),""),replace_list)
print repn(string,replace_list)
Hey ,don't downvote. I am just want to share some new idea.

I am thinking about a solution for this. First I would make the string input as a list. Then I would replace the items of list. Then through using join command, I will return list as a string. The code can be like this:
def the_replacer(text):
test = []
for m in range(len(text)):
test.append(text[m])
if test[m]==','\
or test[m]=='!'\
or test[m]=='.'\
or test[m]=='\''\
or test[m]==';':
#....
test[n]=''
return ''.join(test)
This would remove anything from the string. What do you think about that?

Here is a more_itertools approach:
import more_itertools as mit
s = "A.B!C?D_E#F#"
blacklist = ".!?_##"
"".join(mit.flatten(mit.split_at(s, pred=lambda x: x in set(blacklist))))
# 'ABCDEF'
Here we split upon items found in the blacklist, flatten the results and join the string.

Python 3, single line list comprehension implementation.
from string import ascii_lowercase # 'abcdefghijklmnopqrstuvwxyz'
def remove_chars(input_string, removable):
return ''.join([_ for _ in input_string if _ not in removable])
print(remove_chars(input_string="Stack Overflow", removable=ascii_lowercase))
>>> 'S O'

Why not utilize this simple function:
def remove_characters(str, chars_list):
for char in chars_list:
str = str.replace(char, '')
return str
Use function:
print(remove_characters('A.B!C?', ['.', '!', '?']))
Output:
ABC

Related

Using string.replace(x, y) to replace all

I have just started to learn python and would like help using string.replace(x,y).
Specifically, replacing all to X's and x's depending whether the letter was originally capitalized or not.
e.g.
John S. Smith -> Xxxx X. Xxxxx
What I have created currently is below.
print("Enter text to translate:", end ="")
sentence = input ()
replaced = sentence.replace("", "x")
print(replaced)
However when I input text like "John Smith". I am returned with "xJxoxhxnx xSx.x xSxmxixtxhx".
Thank you in advance!
Edit: Although string.replace(x,y) may be longer to perform, I'd like to slowly build on my knowledge before finding faster and shorter ways to perform the same operation. I'd highly appreciate it if it was explained in terms of string.replace(x, y) instead of re.sub
Edit2: I have been notified that string.replace is the wrong tool to use. Thank you for your help! I will be reading into re.sub instead.
If you insist on using replace even though it's the wrong tool for the job (because it can only replace one letter at a time and has to go through the whole string every time), here's a way:
>>> s = 'John S. Smith'
>>> for c in s:
if c.islower():
s = s.replace(c, 'x')
if c.isupper():
s = s.replace(c, 'X')
>>> s
'Xxxx X. Xxxxx'
And a somewhat neat more efficient way:
>>> ''.join('x' * c.islower() or 'X' * c.isupper() or c for c in s)
'Xxxx X. Xxxxx'
And a regex way:
>>> re.sub('[A-Z]', 'X', re.sub('[a-z]', 'x', s))
'Xxxx X. Xxxxx'
import re
print("Enter text to translate:", end ="")
sentence = input()
replaced = re.sub("[A-Z]", 'X', re.sub("[a-z]", 'x', sentence))
print replaced
Use re.sub to replace individual character of string or iterate through the string.
Not the correct usecase for string replace.
There are 2 things that you can do:
Loop through the string and perform the operation
Use re.sub to replace using regex. (How to input a regex in string.replace?)
Plain way:
>>> my_string = "John S. Smith"
>>> replaced = ''
>>> for character in my_string:
if character.isupper():
replaced += 'X'
elif character.islower():
replaced += 'x'
else:
replaced += character
>>> replaced
'Xxxx X. Xxxxx'
One liner:
>>> ''.join('x' if c.islower() else 'X' if c.isupper() else c for c in my_string)
'Xxxx X. Xxxxx'
print("Enter text to translate:", end ="")
sentence = input ()
replaced = ''.join(['x' if (i>='a' and i<='z') else 'X' if (i>='A' and i<='Z') else i for i in sentence])
print(replaced)
You can use the re module, and provide re.sub with a callback function to achieve this.
import re
def get_capital(ch):
if ch.isupper():
return "X"
return "x"
def get_capitals(s):
return re.sub("[a-zA-Z]", lambda ch: get_capital(ch.group(0)), s)
print(get_capitals("John S. Smith"))
Has the output:
Xxxx X. Xxxxx
As a matter of style, I've not condensed any of this into lambdas or one-liners, although you probably could.
This will be significantly faster than just repeated concatenation.
** edit fixed code **
Try this instead, it is a bit of a simple and easily breakable example but this is what you would need to achieve what you want:
s = "Testing This"
r = ""
for c in s:
if c.isalpha():
if c.islower():
r += "x"
else:
r += "X"
else:
r += c
print(r)

Is there a way to remove all characters except letters in a string in Python?

I call a function that returns code with all kinds of characters ranging from ( to ", and , and numbers.
Is there an elegant way to remove all of these so I end up with nothing but letters?
Given
s = '##24A-09=wes()&8973o**_##me' # contains letters 'Awesome'
You can filter out non-alpha characters with a generator expression:
result = ''.join(c for c in s if c.isalpha())
Or filter with filter:
result = ''.join(filter(str.isalpha, s))
Or you can substitute non-alpha with blanks using re.sub:
import re
result = re.sub(r'[^A-Za-z]', '', s)
A solution using RegExes is quite easy here:
import re
newstring = re.sub(r"[^a-zA-Z]+", "", string)
Where string is your string and newstring is the string without characters that are not alphabetic. What this does is replace every character that is not a letter by an empty string, thereby removing it. Note however that a RegEx may be slightly overkill here.
A more functional approach would be:
newstring = "".join(filter(str.isalpha, string))
Unfortunately you can't just call stron a filterobject to turn it into a string, that would look much nicer...
Going the pythonic way it would be
newstring = "".join(c for c in string if c.isalpha())
You didn't mention you want only english letters, here's an international solution:
import unicodedata
str = u"hello, ѱϘяԼϷ!"
print ''.join(c for c in str if unicodedata.category(c).startswith('L'))
Here's another one, using string.ascii_letters
>>> import string
>>> "".join(x for x in s if x in string.ascii_letters)
`
>>> import re
>>> string = "';''';;';1123123!##!##!#!$!sd sds2312313~~\"~s__"
>>> re.sub("[\W\d_]", "", string)
'sdsdss'
Well, I use this for myself in this kind of situations
Sorry, if it's outdated :)
string = "The quick brown fox jumps over the lazy dog!"
alphabet = "abcdefghijklmnopqrstuvwxyz"
def letters_only(source):
result = ""
for i in source.lower():
if i in alphabet:
result += i
return result
print(letters_only(string))
s = '##24A-09=wes()&8973o**_##me'
print(filter(str.isalpha, s))
# Awesome
About return value of filter:
filter(function or None, sequence) -> list, tuple, or string

Analyzing string input until it reaches a certain letter on Python

I need help in trying to write a certain part of a program.
The idea is that a person would input a bunch of gibberish and the program will read it till it reaches an "!" (exclamation mark) so for example:
input("Type something: ")
Person types: wolfdo65gtornado!salmontiger223
If I ask the program to print the input it should only print wolfdo65gtornado and cut anything once it reaches the "!" The rest of the program is analyzing and counting the letters, but those part I already know how to do. I just need help with the first part. I been trying to look through the book but it seems I'm missing something.
I'm thinking, maybe utilizing a for loop and then placing restriction on it but I can't figure out how to make the random imputed string input be analyzed for a certain character and then get rid of the rest.
If you could help, I'll truly appreciate it. Thanks!
The built-in str.partition() method will do this for you. Unlike str.split() it won't bother to cut the rest of the str into different strs.
text = raw_input("Type something:")
left_text = text.partition("!")[0]
Explanation
str.partition() returns a 3-tuple containing the beginning, separator, and end of the string. The [0] gets the first item which is all you want in this case. Eg.:
"wolfdo65gtornado!salmontiger223".partition("!")
returns
('wolfdo65gtornado', '!', 'salmontiger223')
>>> s = "wolfdo65gtornado!salmontiger223"
>>> s.split('!')[0]
'wolfdo65gtornado'
>>> s = "wolfdo65gtornadosalmontiger223"
>>> s.split('!')[0]
'wolfdo65gtornadosalmontiger223'
if it doesnt encounter a "!" character, it will just grab the entire text though. if you would like to output an error if it doesn't match any "!" you can just do like this:
s = "something!something"
if "!" in s:
print "there is a '!' character in the context"
else:
print "blah, you aren't using it right :("
You want itertools.takewhile().
>>> s = "wolfdo65gtornado!salmontiger223"
>>> '-'.join(itertools.takewhile(lambda x: x != '!', s))
'w-o-l-f-d-o-6-5-g-t-o-r-n-a-d-o'
>>> s = "wolfdo65gtornado!salmontiger223!cvhegjkh54bgve8r7tg"
>>> i = iter(s)
>>> '-'.join(itertools.takewhile(lambda x: x != '!', i))
'w-o-l-f-d-o-6-5-g-t-o-r-n-a-d-o'
>>> '-'.join(itertools.takewhile(lambda x: x != '!', i))
's-a-l-m-o-n-t-i-g-e-r-2-2-3'
>>> '-'.join(itertools.takewhile(lambda x: x != '!', i))
'c-v-h-e-g-j-k-h-5-4-b-g-v-e-8-r-7-t-g'
Try this:
s = "wolfdo65gtornado!salmontiger223"
m = s.index('!')
l = s[:m]
To explain accepted answer.
Splitting
partition() function splits string in list with 3 elements:
mystring = "123splitABC"
x = mystring.partition("split")
print(x)
will give:
('123', 'split', 'ABC')
Access them like list elements:
print (x[0]) ==> 123
print (x[1]) ==> split
print (x[2]) ==> ABC
Suppose we have:
s = "wolfdo65gtornado!salmontiger223" + some_other_string
s.partition("!")[0] and s.split("!")[0] are both a problem if some_other_string contains a million strings, each a million characters long, separated by exclamation marks. I recommend the following instead. It's much more efficient.
import itertools as itts
get_start_of_string = lambda stryng, last, *, itts=itts:\
str(itts.takewhile(lambda ch: ch != last, stryng))
###########################################################
s = "wolfdo65gtornado!salmontiger223"
start_of_string = get_start_of_string(s, "!")
Why the itts=itts
Inside of the body of a function, such as get_start_of_string, itts is global.
itts is evaluated when the function is called, not when the function is defined.
Consider the following example:
color = "white"
get_fleece_color = lambda shoop: shoop + ", whose fleece was as " + color + " as snow."
print(get_fleece_color("Igor"))
# [... many lines of code later...]
color = "pink polka-dotted"
print(get_fleece_color("Igor's cousin, 3 times removed"))
The output is:
Igor, whose fleece was white as snow.
Igor's cousin, 3 times removed Igor, whose fleece was as pink polka-dotted as snow.
You can extract the beginning of a string, up until the first delimiter is encountered, by using regular expressions.
import re
slash_if_special = lambda ch:\
"\\" if ch in "\\^$.|?*+()[{" else ""
prefix_slash_if_special = lambda ch, *, _slash=slash_if_special: \
_slash(ch) + ch
make_pattern_from_char = lambda ch, *, c=prefix_slash_if_special:\
"^([^" + c(ch) + "]*)"
def get_string_up_untill(x_stryng, x_ch):
i_stryng = str(x_stryng)
i_ch = str(x_ch)
assert(len(i_ch) == 1)
pattern = make_pattern_from_char(ch)
m = re.match(pattern, x_stryng)
return m.groups()[0]
An example of the code above being used:
s = "wolfdo65gtornado!salmontiger223"
result = get_string_up_untill(s, "!")
print(result)
# wolfdo65gtornado
We can use itertools
s = "wolfdo65gtornado!salmontiger223"
result = "".join(itertools.takewhile(lambda x : x!='!' , s))
>>"wolfdo65gtornado"

python string manipulation [duplicate]

I have a string s with nested brackets: s = "AX(p>q)&E((-p)Ur)"
I want to remove all characters between all pairs of brackets and store in a new string like this: new_string = AX&E
i tried doing this:
p = re.compile("\(.*?\)", re.DOTALL)
new_string = p.sub("", s)
It gives output: AX&EUr)
Is there any way to correct this, rather than iterating each element in the string?
Another simple option is removing the innermost parentheses at every stage, until there are no more parentheses:
p = re.compile("\([^()]*\)")
count = 1
while count:
s, count = p.subn("", s)
Working example: http://ideone.com/WicDK
You can just use string manipulation without regular expression
>>> s = "AX(p>q)&E(qUr)"
>>> [ i.split("(")[0] for i in s.split(")") ]
['AX', '&E', '']
I leave it to you to join the strings up.
>>> import re
>>> s = "AX(p>q)&E(qUr)"
>>> re.compile("""\([^\)]*\)""").sub('', s)
'AX&E'
Yeah, it should be:
>>> import re
>>> s = "AX(p>q)&E(qUr)"
>>> p = re.compile("\(.*?\)", re.DOTALL)
>>> new_string = p.sub("", s)
>>> new_string
'AX&E'
Nested brackets (or tags, ...) are something that are not possible to handle in a general way using regex. See http://www.amazon.de/Mastering-Regular-Expressions-Jeffrey-Friedl/dp/0596528124/ref=sr_1_1?ie=UTF8&s=gateway&qid=1304230523&sr=8-1-spell for details why. You would need a real parser.
It's possible to construct a regex which can handle two levels of nesting, but they are already ugly, three levels will already be quite long. And you don't want to think about four levels. ;-)
You can use PyParsing to parse the string:
from pyparsing import nestedExpr
import sys
s = "AX(p>q)&E((-p)Ur)"
expr = nestedExpr('(', ')')
result = expr.parseString('(' + s + ')').asList()[0]
s = ''.join(filter(lambda x: isinstance(x, str), result))
print(s)
Most code is from: How can a recursive regexp be implemented in python?
You could use re.subn():
import re
s = 'AX(p>q)&E((-p)Ur)'
while True:
s, n = re.subn(r'\([^)(]*\)', '', s)
if n == 0:
break
print(s)
Output
AX&E
this is just how you do it:
# strings
# double and single quotes use in Python
"hey there! welcome to CIP"
'hey there! welcome to CIP'
"you'll understand python"
'i said, "python is awesome!"'
'i can\'t live without python'
# use of 'r' before string
print(r"\new code", "\n")
first = "code in"
last = "python"
first + last #concatenation
# slicing of strings
user = "code in python!"
print(user)
print(user[5]) # print an element
print(user[-3]) # print an element from rear end
print(user[2:6]) # slicing the string
print(user[:6])
print(user[2:])
print(len(user)) # length of the string
print(user.upper()) # convert to uppercase
print(user.lstrip())
print(user.rstrip())
print(max(user)) # max alphabet from user string
print(min(user)) # min alphabet from user string
print(user.join([1,2,3,4]))
input()

Python strip() multiple characters?

I want to remove any brackets from a string. Why doesn't this work properly?
>>> name = "Barack (of Washington)"
>>> name = name.strip("(){}<>")
>>> print name
Barack (of Washington
Because that's not what strip() does. It removes leading and trailing characters that are present in the argument, but not those characters in the middle of the string.
You could do:
name= name.replace('(', '').replace(')', '').replace ...
or:
name= ''.join(c for c in name if c not in '(){}<>')
or maybe use a regex:
import re
name= re.sub('[(){}<>]', '', name)
I did a time test here, using each method 100000 times in a loop. The results surprised me. (The results still surprise me after editing them in response to valid criticism in the comments.)
Here's the script:
import timeit
bad_chars = '(){}<>'
setup = """import re
import string
s = 'Barack (of Washington)'
bad_chars = '(){}<>'
rgx = re.compile('[%s]' % bad_chars)"""
timer = timeit.Timer('o = "".join(c for c in s if c not in bad_chars)', setup=setup)
print "List comprehension: ", timer.timeit(100000)
timer = timeit.Timer("o= rgx.sub('', s)", setup=setup)
print "Regular expression: ", timer.timeit(100000)
timer = timeit.Timer('for c in bad_chars: s = s.replace(c, "")', setup=setup)
print "Replace in loop: ", timer.timeit(100000)
timer = timeit.Timer('s.translate(string.maketrans("", "", ), bad_chars)', setup=setup)
print "string.translate: ", timer.timeit(100000)
Here are the results:
List comprehension: 0.631745100021
Regular expression: 0.155561923981
Replace in loop: 0.235936164856
string.translate: 0.0965719223022
Results on other runs follow a similar pattern. If speed is not the primary concern, however, I still think string.translate is not the most readable; the other three are more obvious, though slower to varying degrees.
string.translate with table=None works fine.
>>> name = "Barack (of Washington)"
>>> name = name.translate(None, "(){}<>")
>>> print name
Barack of Washington
Because strip() only strips trailing and leading characters, based on what you provided. I suggest:
>>> import re
>>> name = "Barack (of Washington)"
>>> name = re.sub('[\(\)\{\}<>]', '', name)
>>> print(name)
Barack of Washington
strip only strips characters from the very front and back of the string.
To delete a list of characters, you could use the string's translate method:
import string
name = "Barack (of Washington)"
table = string.maketrans( '', '', )
print name.translate(table,"(){}<>")
# Barack of Washington
Since strip only removes characters from start and end, one idea could be to break the string into list of words, then remove chars, and then join:
s = 'Barack (of Washington)'
x = [j.strip('(){}<>') for j in s.split()]
ans = ' '.join(j for j in x)
print(ans)
For example string s="(U+007c)"
To remove only the parentheses from s, try the below one:
import re
a=re.sub("\\(","",s)
b=re.sub("\\)","",a)
print(b)

Categories

Resources