Write A Python Function To Test Whether The Two Words Start With The Same Character Or Not

Given two words that are two strings your task is to compare the first letter of both the strings.  if the first  character of the both the string is the same then return true else return false.

You can also print in the function itself,  if the first character is the same or not.

 

Write A Python Function To Test Whether The Two Words Start With The Same Character Or Not

 

 


Write A Python Function To Test Whether The Two Words Start With The Same Character Or Not

Define a function compare() having the two input sring values first and second. Use the if statement and check if the first character of both the string is the same then return true,  else return false.

def compare(first, second):
   if first[0] == second[0]:
       return True
   return False




In the main program take two string variables, ask user to enter two strings using the the input function call. Take a flag and call the function to compare two strings by passing the input string values,  once the function is executed  check flag variable.

If the flag is set to true then print both the string starts with same character.  if the flag is false then print both the string doesn't start with same character

string1 = input("Enter the first string : ")
string2 = input("Enter the second string : ")
flag = compare(string1, string2)
if flag:
   print("Both words start with same Character")
else:
   print("Both words doesn't start with same Character")





Full program
================

def compare(first, second):
   if first[0] == second[0]:
       return True
   return False


string1 = input("Enter the first string : ")
string2 = input("Enter the second string : ")
flag = compare(string1, string2)
if flag:
   print("Both words start with same Character")
else:
   print("Both words does not start with same Character")




Output: Positive scenario
=====================

Enter the first string : hello
Enter the second string : hi
Both words start with same Character




Output: Negative scenario
=====================

Enter the first string : 1234
Enter the second string : hi
Both words does not start with same Character




Write A Python Function To Test Whether The Two Words Start With The Same Character Or Not

The above program can also be written as shown below,  without returning anything and just comparing the first character and printing the result.


Full program
===============

def compare(first, second):
   if first[0] == second[0]:
       print("Both Strings start with same Character")
   else:
       print("Both Strings does not start with same Character")


string1 = input("Enter the first string : ")
string2 = input("Enter the second string : ")
compare(string1, string2)




Conclusion
=================

Run the program by yourself providing the random first and second words as an input.  Compare the results for positive and negative scenarios.



Post a Comment

0 Comments