Limit execution to main module
It is customary for code that starts executing a series of commands to be surrounded in a special if
-block:
if __name__ == '__main__':
...
This prevents the code from being executed when it is imported into another module.
It's probably a good idea to put most of your code into a method or two
Particularly once you've put your code inside a main block, the multiple levels of indentation can get a little messy quickly. It helps to put some of the code into a method and then call it, rather than just have it all in sequence:
def print_longer_string(s1, s2):
...
string1 = input("Enter first string: ")
string2 = input("Enter second string: ")
print_longer_string(string1, string2)
Use len
len
is the standard mechanism for obtaining the length of a str
, as well as any other sequence type.
Reduce repetition
You can reduce your if
block to just two conditions by testing for equal lengths first and using a ternary:
if len(string1) == len(string2):
print("Both strings are equal.")
else:
print("Larger string is:")
print(string1 if len(string1) > len(string2) else string2)
This allows you to avoid repeating the print("Larger string is:")
line without having to move that message to a variable.
Use more descriptive messages
"Both strings are equal" doesn't really describe what the program is telling you. "Larger" can also have different meanings, as well. (It could refer to lexical sorting, for example.) "The strings have equal length" and "The longer string is:" would be more explicit and less likely to cause confusion. We could differentiate between character and byte length, if that won't be clear from context, but character length is the usual assumption and is what you get from Python 3 by default.
Formatting
Read PEP8 for Python's standards on the use of spaces around parentheses, indentation length, and blank lines. Your team might define their own standards, but PEP8 is the industry default.
Final code
Putting all these together, you will get something like
def print_longer_string(s1, s2):
if len(s1) == len(s2):
print("The strings have equal length")
else:
print("The longer string is:")
print(s1 if len(s1) > len(s2) else s2)
if __name__ == '__main__':
s1 = input("Enter the first string: ")
s2 = input("Enter the second string: ")
print_longer_string(s1, s2)
You'll note I also shortened the variables down to s1
and s2
. string1
is actually fine as a variable name if you prefer; I just find s1
a bit quicker to read through. You usually want meaningful variable names, but there's no semantic meaning to these variables to capture in the name since it's just two arbitrary strings, so s1
doesn't really lose anything over string1
.
I also want to note that I considered separating out the print
ing from actually picking which string to print. I decided not to separate them because the case of equal lengths was handled differently. This fact greatly reduced any benefit we would get from separating the determination from the actual IO call. Separating them would require either having a function that returns the full string to print (which has little value since the exact message is probably dependent on the IO mechanism anyway) or introducing an extra indicator in the return value to detect the equal length case (which is a level of complexity the program does not need yet under its current requirements).
"aa"
is larger than"z"
. Normally we'd use the word "longer" to make it 100% clear we're talking just about length, not some other comparison predicate like alphabetical order, i.e. first mismatching character. \$\endgroup\$