Check sibling questions
Computer Science - Class 12
Chapter 14 - Functions in python

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

Are ads bothering you?


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’

CA Maninder Singh's photo - Expert in Practical Accounts, Taxation and Efiling

Made by

CA Maninder Singh

CA Maninder Singh is a Chartered Accountant for the past 12 years. He also provides Accounts Tax GST Training in Delhi, Kerala and online.