Python: Program to Compare Two Strings
Python: Program to Compare Two Strings

Python: Program to Compare Two Strings

Learn how to compare strings in Python using multiple methods, including the == operator and other built-in functions. Explore detailed explanations and algorithms for efficient string comparison.

This Python program demonstrates various methods to compare two strings to determine if they are equal or not.

Method 1: Using the == Operator

Algorithm for Python Program:

Plaintext
Step 1: Start
Step 2: Input: Two strings, str1 and str2.
Step 3: Process:
        (i) Use the == operator to check if str1 is equal to str2.
        (ii) Print a message indicating whether the strings are equal      
             or not based on the comparison result.
Step 4: Output: Print the comparison result.
Step 5: Exit

Pseudocode:

Python
function compare_strings_equal(str1, str2):
    if str1 == str2:
        print("Strings are equal")
    else:
        print("Strings are not equal")

Program Example:

Python
def compare_strings_equal(str1, str2):
    if str1 == str2:
        print("Strings are equal")
    else:
        print("Strings are not equal")

# Example usage
str1 = "Hello"
str2 = "Hello"
compare_strings_equal(str1, str2)

Step-by-Step Explanation:

Function Definition (compare_strings_equal):

  • Takes two parameters, str1 and str2, which are the strings to be compared.

Comparison (if str1 == str2):

  • Checks if str1 is equal to str2 using the == operator.

Output:

  • Prints “Strings are equal” if str1 equals str2.
  • Prints “Strings are not equal” if str1 does not equal str2.

Method 2: Using str.compare()

Algorithm for Python Program:

Plaintext
Step 1: Start
Step 2: Input: Two strings, str1 and str2.
Step 3: Process:
        (i) Use the str.compare() method to perform a lexicographical  
        comparison between str1 and str2.
        
        Print a message based on the comparison result:
        (i) If str1 is less than str2, print "str1 is less than str2".
        (ii) If str1 is greater than str2, print "str1 is greater than 
             str2".
        (iii) If str1 equals str2, print "str1 is equal to str2".

Step 4: Output: Print the comparison result.
Step 5: Exit

Pseudocode:

Python
function compare_strings_compare(str1, str2):
    comparison_result = str1.compare(str2)
    if comparison_result < 0:
        print("str1 is less than str2")
    elif comparison_result > 0:
        print("str1 is greater than str2")
    else:
        print("str1 is equal to str2")

Program Example:

Python
def compare_strings_compare(str1, str2):
    comparison_result = str1.compare(str2)
    if comparison_result < 0:
        print("str1 is less than str2")
    elif comparison_result > 0:
        print("str1 is greater than str2")
    else:
        print("str1 is equal to str2")

# Example usage
str1 = "apple"
str2 = "banana"
compare_strings_compare(str1, str2)

Step-by-Step Explanation:

Function Definition (compare_strings_compare):

  • Takes two parameters, str1 and str2, which are the strings to be compared.

Comparison (str1.compare(str2)):

  • Performs a lexicographical comparison between str1 and str2 using the compare() method of strings.

Output:

  • Prints “str1 is less than str2” if str1 is lexicographically less than str2.
  • Prints “str1 is greater than str2” if str1 is lexicographically greater than str2.
  • Prints “str1 is equal to str2” if str1 is lexicographically equal to str2.

Summary:

  • Purpose: To compare two strings in Python using different methods.
  • Input: Two strings, str1 and str2.
  • Output: Messages indicating whether the strings are equal, or their lexicographical order relationship.
  • Methods Covered: == operator and str.compare() method.
  • Algorithm Efficiency: O(n) for both methods, where n is the length of the longer string among str1 and str2.

Discover more from lounge coder

Subscribe to get the latest posts sent to your email.

Leave a Reply

Your email address will not be published. Required fields are marked *

Discover more from lounge coder

Subscribe now to keep reading and get access to the full archive.

Continue reading