Computer >> Computer tutorials >  >> Programming >> Python

Python Program to Take in Two Strings and Display the Larger String without Using Built-in Functions


When it is required to take two strings, and display the larger string without using any built-in function, a simple iteration and the ‘==’ operator can be used.

Below is the demonstration of the same −

Example

string_1 = "Malala"
string_2 = "Male"
count_1 = 0
count_2 = 0
print("The first string is :")
print(string_1)
print("The second string is :")
print(string_2)
for i in string_1:
   count_1 = count_1+1
for j in string_2:
   count_2 = count_2+1
if(count_1<count_2):
   print("The larger string is:")
   print(string_2)
elif(count_1==count_2):
   print("Both the strings are equal.")
else:
   print("The larger string is:")
   print(string_1)

Output

The first string is :
Malala
The second string is :
Male
The larger string is:
Malala

Explanation

  • Two strings are defined, and are displayed on the console.

  • Two counters are initialized to 0.

  • The strings are iterated over, and their lengths are obtained.

  • This is incremented and stored in the counter.

  • Depending on the count value, the larger of both the strings is displayed on the console.