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

PythonServer Side ProgrammingProgramming

When it is required to take two strings and display the larger string without using any built-in function, the counter can be used to get the length of the strings, and ‘if’ condition can be used to compare their lengths.

Below is the demonstration of the same −


 Live Demo

string_1= "Hi there"
string_2= "Hi how are ya"
print("The first string is :")
print("The second string is :")
count_1 = 0
count_2 = 0
for i in string_1:
for j in string_2:
   print("The larger string is :")
   print("Both the strings are equal in length")
   print("The larger string is :")


The first string is :
Hi there
The second string is :
Hi how are ya
The larger string is :
Hi how are ya


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

  • Two counter variables are initialized to 0.

  • The first string is iterated over and its length is determined by incrementing the counter.

  • The same is done for the second string as well.

  • These counts are compared to each other.

  • Depending on the value, the output is displayed on the console.

Published on 17-Apr-2021 12:00:08