why "a" is bigger than "A" in python? [duplicate] - python

This question already has answers here:
How are strings compared?
(7 answers)
Closed 1 year ago.
print(4 > 5)
output is False
this is very easy to understand using basic math
print("a" > "A")
output is True
how does python compare a and A ?

Python string comparison is performed using the characters in both strings. The characters in both strings are compared one by one. When different characters are found then their Unicode value is compared. The character with lower Unicode value is considered to be smaller.
The Unicode value of 'A' is 65, whereas the for 'a' it is 97.
The ord() function returns the Unicode value of a character.
ord('A') # returns 65
ord('a') # returns 97
ord('AA') # ERROR: ord() expects a string of length 1.

"a" ascii is 97 --> ord("a")
"A" ascii is 65 --> ord("A")
Hence:
print("a" > "A") --> True

Related

Why does count of empty string in Python strings return len(str) + 1? [duplicate]

This question already has answers here:
Why are str.count('') and len(str) giving different output?
(3 answers)
Closed 2 years ago.
The count function of strings return the number of non-overlapping occurrences of substring. However, when I try to count empty string in non-empty or empty string, it does not give 0, but len(str) + 1.
>>> 'aaa'.count('') # it should have been 0
>>> 4
>>> ''.count('') # it should have been 0
>>> 1
What is the logic behind this?
That's because empty strings are considered to exist between all the
characters of a string; for a string length 2, there are 3 empty
strings; one at the start, one between the two characters, and one at
the end.
original answer

Converting each individual letter of a string to ASCII equivalent - Python [duplicate]

This question already has answers here:
How to get the ASCII value of a character
(5 answers)
Closed 2 years ago.
How would one write a code to display the conversion of individual letters in a string to it's ASCII equivalent? One example of the output in shell would look like this:
Enter a 3-letter word: Hey
H = 72
e = 101
y = 121
Use built-in ord function
>>> ord('H')
72
Get the input, and print each character plus its ordinal value.
user_input = input("Enter a 3-letter word: ")
for character in user_input:
print(character + " = " + ord(character))

Printing escape sequence character in python [duplicate]

This question already has answers here:
How to get the ASCII value of a character
(5 answers)
Closed 3 years ago.
I tried to print the escape sequence characters or the ASCII representation of numbers in Python in a for loop.
Like:
for i in range(100, 150):
b = "\%d" %i
print(b)
I expected the output like,
A
B
C
Or something.
But I got like,
\100
\101
How to print ASCII representation of the numbers?
There's a builtin function for python called ord and chr
ord is used to get the value of ASCII letter, for example:
print(ord('h'))
The output of the above is 104
ord only support a one length string
chr is inverse of ord
print(chr(104))
The output of the above is 'h'
chr only supports integer. float, string, and byte doesn't support
chr and ord are really important if you want to make a translation of a text file (encoded text file)
You can use the ord() function to print the ASCII value of a character.
print(ord('b'))
> 98
Likewise, you can use the chr() function to print the ASCII character represented by a number.
print(chr(98))
> b

Why is lowercase 'p' greater then uppercase 'P'? [duplicate]

This question already has answers here:
How are strings compared?
(7 answers)
Closed 13 days ago.
print 'Python' > 'python' # equals False
print 'python' > 'Python' # equals True
Can someone please explain how this is interpreted since p is smaller case then then capital P? But yet p is always greater then P.
Tested on Python 2.7
String comparison in Python is case-sensitive and conventionally uppercase characters go before lowercase characters.
Python compares strings lexicographically, using the constituent characters based on their ASCII or Unicode code points. The same principle applies for Python3.
In ASCII, and therefore in Unicode, lowercase letters are greater than all uppercase letters. Therefore, 'p' > 'P', and indeed, 'a' > 'Z'. In your case, "python" begins with the letter 'p', whereas "Python" begins with the uppercase letter 'P'. They begin with different code points; the lowercase variant is greater.
The convention that lowercase letters are greater than uppercase letters in ASCII is historical.
It may have something to do with the unicode values of the letters.
>>> ord('p')
112
>>> ord('P')
80
112 > 80, therefore 'p' > 'P'

python utf-8 behaviour [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Python returning the wrong length of string when using special characters
I read a multilingual string from file in windows-1251, for example s="qwe абв" (second part in Russian), and then:
for i in s.decode('windows-1251').encode('utf-8').split():
print i, len(i)
and I get:
qwe 3
абв 6
Oh God, why? o_O
In programming languages you can't always think of strings as a sequence of characters, because generally they are actually a sequence of bytes. You can't store every character or symbol in 8 bits, character encodings create some rules to combine multiple bytes into a single character.
In the case of the string 'абв' encoded in utf-8, what you have is 6 bytes that represent 3 characters. If you want to count the number of characters instead of the number of bytes, make sure you are taking the length from a unicode string.
>>> print "абв"
абв
>>> print [char for char in "абв"]
['\xd0', '\xb0', '\xd0', '\xb1', '\xd0', '\xb2']
That's why :)

Categories