I'm trying to remove special character from Arabic String using it's Unicode which I got from this link: https://www.fileformat.info/info/unicode/char/0640/index.htm
This is my code:
TATWEEL = u"\u0640"
text = 'الســلام عليكــم'
text.replace(TATWEEL, '')
print(text)
But I tried it and doesn't work (it prints the same string without removing the character)
This is the special character 'ــ'
I'm using Python3
-
3when you say it doesn't work what do you mean? Did you receive an error or the pc you were using exploded?Adelin– Adelin2018年02月13日 05:47:12 +00:00Commented Feb 13, 2018 at 5:47
-
No it prints the same text without removing the character.user8393084– user83930842018年02月13日 05:48:06 +00:00Commented Feb 13, 2018 at 5:48
-
1Possible duplicate of Replacing or substituting in a python string does not workDYZ– DYZ2018年02月13日 06:19:59 +00:00Commented Feb 13, 2018 at 6:19
2 Answers 2
The replace method of strings does not change the string it is called on; it returns a new string with the specified character replaced.
This code does what you want:
TATWEEL = u"\u0640"
text = 'الســلام عليكــم'
text2 = text.replace(TATWEEL, '')
print(text2)
To get the exact result you expected, use this:
text = text.replace(TATWEEL, '')
print(text)
1 Comment
If text may contain multiple unicode elements then you should go for regex as below:
import re
TATWEEL = u"\u0640"
text = 'الســلام عليكــم'
unicode_removed_text = re.sub(TATWEEL, '', text)
4 Comments
replace?replace, use it