-
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(prefix[, start[, end]]):- This method checks if a string starts with the specified
prefix. - Parameters:
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.
- Returns
Trueif the string starts withprefix, otherwiseFalse. - Example:
text = "Hello, world!" if text.startswith("Hello"): print("The string starts with 'Hello'")
- This method checks if a string starts with the specified
-
endswith(suffix[, start[, end]]):- This method checks if a string ends with the specified
suffix. - Parameters:
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.
- Returns
Trueif the string ends withsuffix, otherwiseFalse. - Example:
filename = "script.py" if filename.endswith(".py"): print("The file is a Python script")
- This method checks if a string ends with the specified
-
find(sub[, start[, end]]):- This method searches for the substring
subwithin the string. - Parameters:
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.
- Returns the lowest index in the string where
subis found, or-1ifsubis not found. - Example:
sentence = "Python is powerful" index = sentence.find("is") if index != -1: print(f"'is' found at index {index}")
- This method searches for the substring
Go Back