Tuesday, October 26, 2021

Python not all space are the same

 Lets look at the below 2 strings

'7 октября 2021'

'7 октября 2021'


They look exactly same but are not. Lets try this in Python 

str = '7 октября 2021'

str_1 = '7 октября 2021'

print(str == str_1) -->  you will get false here


so what is the difference, the space is different lets try this 

str = '7 октября 2021'
for s in str:
print("char is {0} and order is {1}".format(s,ord(s)))
str_1 = '7 октября 2021'

for s in str_1:
print("char is {0} and order is {1}".format(s,ord(s)))


the space in str is 

char is   and order is 160

while the space in str_1 is

char is   and order is 32


So if you try str.replace(' ',"/") it does not work. You need to try differently like this

str.replace(chr(160),'/')

No comments: