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.
Related
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:
How does Python 2 compare string and int? Why do lists compare as greater than numbers, and tuples greater than lists?
(2 answers)
Closed 4 years ago.
I have discovered that a list is greater than a number.
>>> [1,2,3] > 1000
True
Is there some reason why this works? I can't convert a list to an int with int([1,2,3]). The int can't be converted to a list with list(1000). So how is python comparing the two?
In this case of "mismatched" types, the types are listed lexicographically by type name: a "list" comes after an "int" in alphabetical ordering, so it is greater.
CPython implementation detail: Objects of different types except
numbers are ordered by their type names; objects of the same types
that don’t support proper comparison are ordered by their address.
(source)
There is no language specification for the ordering (apart from the fact that it is consistent). It just happens to be the case that CPython is the most common implementation in which there is this language detail of being ordered lexicographically by type names.
According to the Python Reference Manual,
Most other objects of built-in types compare unequal unless they are the same object; the choice whether one object is considered smaller or larger than another one is made arbitrarily but consistently within one execution of a program.
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 2 years ago.
When a string is being compared to an integer are the string and int compared with the ASCII code internally, or how is it? I know that strings compare greater than integers, but how does that internal comparison takes place?
>>> "a" > 1
True
In your example, 1 < "a" because "i" for int comes alphabetically before "s" for string.
From the docs:
Objects of different types, except different numeric types and
different string types, never compare equal; such objects are ordered
consistently but arbitrarily (so that sorting a heterogeneous array
yields a consistent result).
I believe this was one of the things changed in python 3 (you would get a TypeError here).
As for how it is done in CPython, objects of different types except numbers are ordered by their type names; objects of the same types that don’t support proper comparison are ordered by their address. Note that this is part of the implementation, not a part of the language.
You should check the source of the __gt__ method of the inbuilt string object to know the details but my guess is that the 1 is converted to a string using the str function and then then the two are compared.
This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
reverse a string in Python
I tried searching the python documentation for a certain slicing exapmle
lets say I have this string
a = "ABCD"
when I write:
a[::-1]
I get the reversed string
"DCBA"
I can't understand how exectaly it works. None of the examples I saw were with two colons. what does it mean? how does it work?
thank you!
The full syntax of a slicing is
a[start:stop:step]
The step value denotes by how much to increase the index when going from one element of the slice to the next one. A value of -1 consequently means "go backwards". If start and stop are omitted as in your example, the default is to use the whole string (or more generally, the whole sequence, as this also works for lists and tuples).