Write a program that uses a function which takes two string arguments and returns the string comparison result of the two passed strings.

 

Answer:

  def string_compare (str1,str2):

     if len(str1)!=len(str2):

        return False

 

    else :

        for i in range(len(str1)):

            if str1[i] != str2[i]:

                return False

            else :

                return True

 

string1=input( "Enter string 1: " )

string2=input( "Enter string 2: " )

f=string_compare(string1,string2)

 

if f == True :

    print( "Strings are same" )

else :

    print( "Strings are different" )

Question 3-Write a program that - Teachoo.JPG

Answer 3-Write a program that - Teachoo.JPG

Remove Ads
Teachoo · Class 12 Explore Class 12

Transcript

Question 3 Write a program that uses a function which takes two string arguments and returns the string comparison result of the two passed strings. Defining a function to compare 2 strings If the length of both strings are not equal, then the function returns false If at any point the characters of the strings are not equal at the same position, then the function returns false Takes 2 strings as user input and assigns it to variables ‘string1’ and ‘string2’ output Checking if the length of both the strings are equal For loop which iterates from 0 to the length of the string If the strings are equal, then the function returns true Invoking function ‘string_compare’ and assigning the value returned to the variable ‘f’

Davneet Singh's photo - Co-founder, Teachoo

Made by

Davneet Singh

Davneet Singh is an IIT Kanpur graduate and has been teaching for 16+ years. At Teachoo, he breaks down Maths, Science and Computer Science into simple steps so students understand concepts deeply and score with confidence.

Many students prefer Teachoo Black for a smooth, ad-free learning experience.