0

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 'ــ'

enter image description here

I'm using Python3

asked Feb 13, 2018 at 5:44
3
  • 3
    when you say it doesn't work what do you mean? Did you receive an error or the pc you were using exploded? Commented Feb 13, 2018 at 5:47
  • No it prints the same text without removing the character. Commented Feb 13, 2018 at 5:48
  • 1
    Possible duplicate of Replacing or substituting in a python string does not work Commented Feb 13, 2018 at 6:19

2 Answers 2

8

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)
answered Feb 13, 2018 at 5:51
Sign up to request clarification or add additional context in comments.

1 Comment

@user8393084 If it works for you, please accept it as answer.
1

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)
answered Feb 13, 2018 at 6:00

4 Comments

How is this better than replace?
refer this ink; it's very good explanation on where to use replace() and where re.sub()
First answer says if you can use replace, use it
I still don't understand the rationale from your post "If text may contain multiple unicode elements then you should go for regex"

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.