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?
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:
How to convert string representation of list to a list
(19 answers)
Closed 11 months ago.
Say the string is like a = "['Hello', 'World']". After the conversion, a = ['Hello', 'World'] and the type is a list.
It's called expression evaluation. Read about Python's eval() and literal_eval().
Notice that it may be dangerous, so read the docs carefully.
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:
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
This question already has answers here:
string.upper(<str>) and <str>.upper() won't execute
(3 answers)
Closed 8 years ago.
I have been looking around for an answer to this and what I could find was you use the .upper() command but this doesn't seem to work,
Example,
>>> x = a
>>>x.upper()
>>>print (x)
But what it displays is just the original.
The correct way to do it is
x = x.upper()
Python strings are immutable, so str.upper() returns the result instead of changing the string in place (as do all other string manipulation functions).
x.upper() does not modify x; it just computes the new string, which you'd see if you printed x.upper(). To retain that value, you could assign it back to x.