Back to Python
2026-03-165 min read

STRCMP (Python Programming)

Learn STRCMP (Python Programming) step by step with clear examples and exercises.

Title: Python STRCMP Function - A full guide

Why This Matters

In programming, comparing strings is a common task. While the STRCMP function in C and other languages provides a direct method for string comparison, Python does not have an equivalent built-in function. However, we can achieve similar functionality using Python's built-in functions and understanding how to compare strings effectively can help you solve real-world problems, debug complex issues, and excel in programming interviews.

Prerequisites

Before diving into the STRCMP equivalent in Python, make sure you have a solid understanding of the following:

  1. Basic Python syntax
  2. Variables and data types
  3. Control structures (if-else statements)
  4. String manipulation functions (e.g., len(), index(), split())
  5. List comprehensions
  6. Understanding of tuples, lists, and dictionaries
  7. Exception handling using try-except blocks
  8. Basic file I/O operations
  9. Understanding the difference between the == operator and the deprecated cmp() function
  10. Knowledge about data structures like stacks, queues, and sets

Core Concept

Python doesn't have a built-in function like C's STRCMP. However, we can compare strings using the built-in == operator or the deprecated cmp() function (which is deprecated as of Python 3.x). To achieve case-insensitive comparison, we can convert both strings to uppercase or lowercase before comparing them.

Here's a simple example:

str1 = "Hello"
str2 = "hello"

Case-sensitive comparison using == operator

if str1 == str2:

print("Strings are equal (case sensitive)")

else:

print("Strings are not equal (case sensitive)")

Case-insensitive comparison using lower() method

if str1.lower() == str2.lower():

print("Strings are equal (case insensitive)")

else:

print("Strings are not equal (case insensitive)")


### Comparing Lists of Strings

To compare lists of strings, you can use a loop to iterate through each pair of elements and perform the comparison. Here's an example:

list1 = ["Hello", "world", "Python"]

list2 = ["hello", "WORLD", "python"]

if len(list1) != len(list2):

print("Lists are not equal in length")

else:

for i in range(len(list1)):

if list1[i].lower() != list2[i].lower():

print("Lists are not equal (case insensitive)")

break

else:

print("Lists are equal (case insensitive)")

Worked Example

Let's consider a practical example where we need to compare two strings, user_input1 and user_input2, entered by the user. We want to check if they are equal case-insensitively and handle potential exceptions such as when the user enters non-string input.

Get user input

try:

user_input1 = input("Enter the first string: ")

user_input2 = input("Enter the second string: ")

except ValueError:

print("Please enter valid strings.")

else:

Case-insensitive comparison

if user_input1.lower() == user_input2.lower():

print("The strings are equal (case insensitive)")

else:

print("The strings are not equal (case insensitive)")

Common Mistakes

  1. Forgetting to convert both strings to the same case before comparison.
  2. Using the deprecated cmp() function instead of the == operator or converting strings to the same case.
  3. Comparing strings with different data types (e.g., comparing a string with an integer).
  4. Forgetting to handle edge cases, such as when one or both strings are empty or non-string input.
  5. Not accounting for whitespace when comparing strings (e.g., leading/trailing spaces, multiple spaces between characters).
  6. Comparing strings using == operator instead of is operator, which checks if the variables point to the same object in memory.
  7. Using the == operator to compare strings with different lengths.
  8. Not considering Unicode normalization when comparing strings with international characters.

Common Mistakes - Edge Cases

  1. Handling empty strings:
if not (user_input1 or user_input2):
print("Both strings are empty.")
else:
...
  1. Handling leading/trailing whitespace:
user_input1 = user_input1.strip() # Removes leading and trailing whitespaces

Practice Questions

  1. Write a Python function that takes two arguments and returns True if the arguments are equal case-insensitively, and False otherwise.
  2. Given the following list of strings: ["Python", "is", "awesome"], write code to print only the words that appear more than once in the list (case insensitive).
  3. Write a Python script that asks the user for two file names, reads the contents of both files, and prints whether they are identical or not (case-insensitive).
  4. Write a Python function that checks if a given string is a palindrome (case insensitive) by comparing it with its reverse.
  5. Write a Python script that compares two lists of strings, returning the list of strings that appear in both lists (case insensitive).
  6. Write a Python script that reads a file containing multiple lines and checks if any two lines are identical (case-insensitive).
  7. Write a Python function that finds the longest common substring between two strings (case insensitive).
  8. Write a Python script that compares two dictionaries, returning the keys that have the same values in both dictionaries (case insensitive).
  9. Write a Python function that sorts a list of strings lexicographically, considering case and whitespace (e.g., "Python" should come before "python", and " Py " should come before "python").
  10. Write a Python script that reads a file containing multiple lines and removes all duplicate lines (case insensitive).

FAQ

Q: Why can't I use the STRCMP function in Python?

A: Python doesn't have a built-in STRCMP function like C or other languages. However, you can achieve similar functionality using built-in functions and converting strings to the same case before comparison.

Q: What is the difference between == and is for string comparison in Python?

A: The == operator compares two strings for equality (case sensitive), while the is operator checks if the variables point to the same object in memory. To compare strings case-insensitively, convert both strings to the same case before comparison or use the == operator.

Q: How can I check if a string is a palindrome in Python?

A: You can check if a string is a palindrome by comparing it with its reverse (case insensitive), using the following code:

def is_palindrome(s):
s = s.lower()
return s == s[::-1]

Q: How can I handle international characters when comparing strings in Python?

A: To ensure proper comparison of international characters, you should normalize the strings using Unicode normalization forms (NFC or NFKD) before comparison. You can use the unicodedata.normalize() function for this purpose.

Q: Why is it important to handle edge cases when comparing strings in Python?

A: Handling edge cases ensures that your code works correctly under various conditions, such as empty strings, leading/trailing whitespace, and international characters. This helps avoid unexpected behavior and improves the robustness of your code.

STRCMP (Python Programming) | Python | XQA Learn