3

I want to replace the number sign (#) with a character similar to that called music sharp sign (♯). I tried the following line but didnt work.

res['n'].replace('#', '♯')

I also tried these and also didnt work fine.

res['n'].replace('#', u'♯')
res['n'].replace('#', '\xe2')

Anyone got any idea about the situation?

6
  • 2
    It "didnt work". What was the error? It works perfectly fine to me. What is res? Commented Dec 31, 2014 at 9:24
  • first one works for me. Commented Dec 31, 2014 at 9:24
  • Maybe you were expecting replace() to modify the string in place? replace() doesn't do that; it creates a new string. You would need to write res['n'] = res['n'].replace('#', '♯'). Commented Dec 31, 2014 at 9:32
  • 1
    ♯ == \xe2\x99\xaf, not only \xe2 Commented Dec 31, 2014 at 9:52
  • Good point @ceremcem: Also, '\xe2\x99\xaf'.decode('utf-8') == u'\u266f'. Commented Dec 31, 2014 at 12:06

3 Answers 3

2
res="He##o"
res=res.replace("#","♯")
print res

o/t He♯♯o
Sign up to request clarification or add additional context in comments.

Comments

0
import re
text = ' '
re.sub(' ', '\x00', text)

Why not you use this?

1 Comment

Why do you think this is even vaguely relevant to the question?
0

Replace returns the changed string, strings cannot be changed themselves. Try

res['n'] = res['n'].replace('#', '♯')

Comments

Your Answer

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.