Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Different storage position of equal strings with special characters [duplicate]

I am new at python and I'm currently exploring some of its core functionalities.

Could you explain me why the following example always return false in case of a string with special characters:

>>> a="x"
>>> b="x"
>>> a is b
True
>>> a="xxx"
>>> b="xxx"
>>> a is b
True
>>> a="xü"
>>> b="xü"
>>> a is b
False
>>> a="ü"
>>> b="ü"
>>> a is b
True
>>> #strange: with one special character it works as expected

I understand that the storage positions are different for strings with special characters on each assignment, I already checked it with the id() function but for which reason python handles strings in this unconsistent way?

like image 850
Nico M Avatar asked Aug 07 '14 12:08

Nico M


People also ask

Can we store special characters in string?

To display them, Java has created a special code that can be put into a string: \". Whenever this code is encountered in a string, it is replaced with a double quotation mark. For example, examine the following: System.

How can I reverse a string without changing the position of special characters in Java?

Simple Solution:Create a temporary character array say temp[]. Copy alphabetic characters from the given array to temp[]. Reverse temp[] using standard string reversal algorithm. Now traverse input string and temp in a single loop.

How do you replace special characters in Regex?

If you are having a string with special characters and want's to remove/replace them then you can use regex for that. Use this code: Regex. Replace(your String, @"[^0-9a-zA-Z]+", "")


1 Answers

Python (the reference implementation at least) has a cache for small integers and strings. I guess unicode strings outside the ASCII range are bigger than the cache threshold (internally unicode is stored using 16 or 32 bit wide characters, UCS-2 or UCS-4) and so they are not cached.

[edit]

Found a more complete answer at: About the changing id of a Python immutable string

Se also: http://www.laurentluce.com/posts/python-string-objects-implementation/

like image 166
Paulo Scardine Avatar answered Oct 07 '22 14:10

Paulo Scardine