Question about quantity and Python

I am practicing simple Python algorithms and I was a little confused by the correct result of one. A particular part of the algorithm in particular wherein two strings of equal length must be greater than the other for the algorithm to function; yet, on the surface the seem equal size-wise.

Code is below:

result=str()
s1 = "Abc"
s2 = "Xyz"
s2 = s2[::-1]
str1_length = len(s1)
str2_length = len(s2)
if str1_length > str2_length:
    biggest = str1_length 
else: biggest = str2_length

My assumption is the greater than status of one string over another on is determined by comparing the ASCII value of individual elements of the different strings to each other. If one ASCII value is higher than another one at one particular index than THAT string is greater than the other one.

Is my assumption correct?

Sorry I only posted the first half of the code.
The rest is below:

for char in range(biggest):
    if char < str1_length:
        result = result + s1[char]
    if char < str2_length:
        result = result + s2[char]
print(result)

The goal is to create a new string made of the first char of s1, then the last char of s2, Next, the second char of s1 and second last char of s2, and so on. Any leftover characters go at the end of the result.
The correct result being: “AzbycX”

Could you clarify your question? Is it regarding specific part of code? In the pasted code there’s no comparison working as described here.

Perhaps I’ve misunderstood what’s happening.
Here:

str2_length = len(s2)
if str1_length > str2_length:
    biggest = str1_length 
else: biggest = str2_length

Actually, I just realized that A would have a lower ASCII scored than z. So I’m really lost about why these two values would be greater than one another. Somehow they are because the code functions when I run it in VSCode. Now I wonder if it’s the combined ASCII scores of each…? I don´t know. I would like some help on clarifying this though.

Both str1_length and str2_length are numbers, they are compared like numbers.

if str1_length > str2_length:
    biggest = str1_length 
else: biggest = str2_length

str1_length > str2_length evaluates to False, as 3 is not greater than 3. Due to that biggest = str1_length is not executed, but as there’s else the code from it is executed - biggest = str2_length.

To summary - the code here doesn’t necessarily answer which one is bigger. It defines what to do when str1_length is bigger than str2_length. And what to do when that’s not a case - then str2_length is either bigger or equal to the str1_length.

Of course. I was getting sidetracked on the idea of comparing them that I never considered that if both variables are equal it is a moot point and it will logically continue to the next consideration. Thank you.

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.