This question already has answers here:
Change in max length of interned strings in CPython
(1 answer)
Python string interning
(2 answers)
Why does comparing strings using either '==' or 'is' sometimes produce a different result?
(15 answers)
Closed 3 years ago.
I know that python's "is" comparison uses the ID of the object to compare, but it seems to behave inconsistently specifically when comparing a string concatenated with itself. Up to 20, for a single character str, it returns True, then False:
>>>'a'*20 is 'a'*20
True
>>>'a'*21 is 'a'*21
False
With strings of different lengths it seems to happen wherever the total string surpasses 21:
>>>'abcdefghijk'*2 is 'abcdefghijk'*2
False
>>>'abcdefghijk'*1 is 'abcdefghijk'*1
True
But this doesn't apply to strings which are already longer than 21 characters:
>>>'abcdefghijklmnopqrstuvwxyz'*1 is 'abcdefghijklmnopqrstuvwxyz'*1
True
What is python doing that causes this change of behavior?
EDIT: I'm using Python 3.6.5
Related
This question already has answers here:
Python string literal concatenation
(1 answer)
String concatenation without '+' operator
(6 answers)
Closed 4 months ago.
I'm using python 3.10.4 right now.
I mistakenly wrote ["somestring" "somestring"] without a , separating the strings. Which evaluated to ["somestringsomestring"] without raising an error (which an error raised was my expectation).
And it seems like it was like that for previous versions.
How should I interpret the process of "string" "string" being evaluated to "stringstring"?
This question already has answers here:
What determines which strings are interned and when? [duplicate]
(3 answers)
About the changing id of an immutable string
(5 answers)
Closed last year.
a="ab d"
b="ab d"
print(id(a))
print(id(b))
sometimes it is getting interned, sometimes not. What is the general rule for interning of a string in version 3.9 of python.
and are float also get interned or not. I am getting different result every time for this too.
c=3.02
d=3.02
print(id(c))
print(id(d))
This question already has answers here:
What determines which strings are interned and when? [duplicate]
(3 answers)
Python - Why not all immutable objects are always cached?
(2 answers)
Closed 2 years ago.
a = 'This is a very long string. This is a very long string. No?'
b = 'This is a very long string. This is a very long string. No?'
print(id(a), id(b))
print(a is b)
On my computer, the results are:
4467772080 4467772080
True
As I know Python only caches short strings. But why such a long string, it still keeps only one copy? In fact, I changed it to be a very long string (even longer than 1024), a and b are still pointing to the same string object. Please correct me where I am wrong?
This question already has answers here:
How does Python 2 compare string and int? Why do lists compare as greater than numbers, and tuples greater than lists?
(2 answers)
Closed 6 years ago.
What is the cause of unintentional infinite loops in my code in Python 2.7 but in Python 3.5 gives me error message?
Python 3.5 gives TypeError: unorderable types: str() > int(), How should I resolve it?
Program
condition = '2'
while condition > 5:
print 'test'
You're trying to compare '2' (a string) to 5 (an int). You should declare without quotes:
condition = 2
This question already has answers here:
making string comparision in python
(2 answers)
Closed 8 years ago.
I am trying to understand what happens when you compare strings with the >,< operators in Python. I know for sure that it doesn't compare the length of the string. Does it compare the sum of their ASCII values?
>>>a='aa'
>>>b='bb'
>>>b>a
True
From docs:
Strings are compared lexicographically using the numeric equivalents
(the result of the built-in function ord()) of their characters.
Unicode and 8-bit strings are fully interoperable in this behavior[4].
The operators are based on the lexicographic order of the strings characters. In your case b[0] > a[0], therefore the statement returns true. If a[0] was equal to b[0] the next character would be compared and so forth.