When learning Python, one of the fundamental tasks is comparing strings. A common scenario is to determine which string is longer. While Python has built-in functions like len()
, understanding how to do this manually can deepen your understanding of loops and basic operations.
In this post, we’ll walk through a simple Python program that compares the length of two strings without using built-in functions.
The Problem
You want to input two strings and determine which one is longer. For example:
- If you input
"apple"
and"banana"
, the program should print"banana"
because it has more characters. - If you input
"cat"
and"dog"
, both have the same length, so either one could be printed.
The Solution
Let’s break down the solution step by step.
- Input the Strings:
We’ll first take two strings as input from the user. - Count the Length of Each String:
Instead of using Python’slen()
function, we’ll use a loop to manually count the number of characters in each string. - Compare the Lengths:
After counting the characters, we’ll compare the lengths and print the string with the greater length.
Here’s the code to achieve this:
# Step 1: Take input from the user
strr1 = input("Enter the first string: ")
strr2 = input("Enter the second string: ")
# Step 2: Initialize counters for each string's length
i = 0
j = 0
# Step 3: Count the number of characters in the first string
for _ in strr1:
i += 1
# Step 4: Count the number of characters in the second string
for _ in strr2:
j += 1
# Step 5: Compare the lengths and print the larger string
if i > j:
print(strr1)
else:
print(strr2)
How Does It Work?
- Step 1: The
input()
function is used to take two strings from the user. These strings are stored in variablesstrr1
andstrr2
. - Step 2: We initialize two counters,
i
andj
, to0
. These will keep track of the lengths ofstrr1
andstrr2
, respectively. - Step 3: We use a
for
loop to go through each character instrr1
. Every time the loop runs, we add 1 toi
, effectively counting the characters in the first string. - Step 4: We repeat the same process for
strr2
using anotherfor
loop and incrementj
for each character. - Step 5: Finally, we compare
i
andj
. Ifi
is greater, it meansstrr1
is longer, so we printstrr1
. Otherwise, we printstrr2
.
Test Cases
Let’s test our program with a few examples to see how it performs:
- Example 1:
- Input:
"hello"
and"world!"
- Output:
"world!"
- Explanation:
"world!"
has 6 characters, while"hello"
has 5.
- Example 2:
- Input:
"python"
and"java"
- Output:
"python"
- Explanation:
"python"
has 6 characters, while"java"
has 4.
- Example 3:
- Input:
"cat"
and"dog"
- Output:
"dog"
(or"cat"
) - Explanation: Both strings have 3 characters, so either one could be printed.
Conclusion
This simple exercise demonstrates how to compare string lengths manually in Python. While built-in functions make tasks like this easier, understanding the logic behind them is essential as you progress in your coding journey. Practice with different strings to solidify your understanding!