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

Go Ad-free

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 has done his B.Tech from Indian Institute of Technology, Kanpur. He has been teaching from the past 14 years. He provides courses for Maths, Science and Computer Science at Teachoo