How to Compare the Length of Two Strings in Python Without Using Built-in Functions

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.

  1. Input the Strings:
    We’ll first take two strings as input from the user.
  2. Count the Length of Each String:
    Instead of using Python’s len() function, we’ll use a loop to manually count the number of characters in each string.
  3. 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?

  1. Step 1: The input() function is used to take two strings from the user. These strings are stored in variables strr1 and strr2.
  2. Step 2: We initialize two counters, i and j, to 0. These will keep track of the lengths of strr1 and strr2, respectively.
  3. Step 3: We use a for loop to go through each character in strr1. Every time the loop runs, we add 1 to i, effectively counting the characters in the first string.
  4. Step 4: We repeat the same process for strr2 using another for loop and increment j for each character.
  5. Step 5: Finally, we compare i and j. If i is greater, it means strr1 is longer, so we print strr1. Otherwise, we print strr2.

Test Cases

Let’s test our program with a few examples to see how it performs:

  1. Example 1:
  • Input: "hello" and "world!"
  • Output: "world!"
  • Explanation: "world!" has 6 characters, while "hello" has 5.
  1. Example 2:
  • Input: "python" and "java"
  • Output: "python"
  • Explanation: "python" has 6 characters, while "java" has 4.
  1. 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!

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top