Search code examples
python-3.xstringstr-replaceregexp-replace

How to replace backslash followed by 2 letters with empty string in Python?


Following is the test string and we need to replace the '\xa' with ''

'FF 6 VRV AVENUE SUBRAMANIYAM PALAYAM PinCode:-\xa0641034'

i was using the following set of lines in python to do the objective but to no use

new_str = str.replace(r'\\xa', '')

but the output is same

'FF 6 VRV AVENUE SUBRAMANIYAM PALAYAM PinCode:-\xa0641034'


Solution

  • I think you are trying to replace the unicode character '\xa0' -

    s = 'FF 6 VRV AVENUE SUBRAMANIYAM PALAYAM PinCode:-\xa0641034' 
    s = s.replace('\xa0', '') 
    print(s)
    #'FF 6 VRV AVENUE SUBRAMANIYAM PALAYAM PinCode:-641034'