Equality Comparison (==):
str1 = "hello"
str2 = "hello"
if str1 == str2:
print("Strings are equal")
Inequality Comparison (!=):
str1 = "hello"
str2 = "world"
if str1 != str2:
print("Strings are not equal")
Case Sensitivity:
str1 = "Hello"
str2 = "hello"
if str1.lower() == str2.lower():
print("Case-insensitive comparison: Strings are equal")
Comparison Operators (<, >, <=, >=):
str1 = "apple"
str2 = "banana"
if str1 < str2:
print("str1 comes before str2 lexicographically")
Length Comparison:
str1 = "apple"
str2 = "banana"
if len(str1) < len(str2):
print("str1 is shorter than str2")
startswith(), endswith(), and find()startswith(prefix[, start[, end]]):
prefix.prefix: Required. The prefix to check against the start of the string.start (optional): Specify where to start the search in the string.end (optional): Specify where to end the search in the string.True if the string starts with prefix, otherwise False.text = "Hello, world!"
if text.startswith("Hello"):
print("The string starts with 'Hello'")
endswith(suffix[, start[, end]]):
suffix.suffix: Required. The suffix to check against the end of the string.start (optional): Specify where to start the search in the string.end (optional): Specify where to end the search in the string.True if the string ends with suffix, otherwise False.filename = "script.py"
if filename.endswith(".py"):
print("The file is a Python script")
find(sub[, start[, end]]):
sub within the string.sub: Required. The substring to search for.start (optional): Specify where to start the search in the string.end (optional): Specify where to end the search in the string.sub is found, or -1 if sub is not found.sentence = "Python is powerful"
index = sentence.find("is")
if index != -1:
print(f"'is' found at index {index}")