Search⌘ K
AI Features

Other Common String Operations

Explore various common string operations in Python including searching for substrings, comparing strings by equality and order, converting case, and removing spaces. This lesson helps you understand how to use these operations effectively in string algorithms, improving your ability to manipulate and analyze text data.

Knowing the basic string operations such as finding the length, traversal, indexing, concatenation, slicing, and splitting is an important start. Knowing additional string operations will make you better prepared for a wider range of string algorithms. A problem may ask whether two strings are equal, whether one string starts with another, whether a smaller string appears inside a larger one, or how to form a new string from existing ones.

Searching

Searching refers to the operation of locating a character or a substring within a larger string. At a fundamental level, this is accomplished by scanning through the string and examining each position until the target is found or the end of the string is reached.

Checking for existence

The simplest form of search determines whether a substring exists anywhere within the string. In Python, this is done using the in operator.

Python 3.14.0
s = "hello world"
# Evaluating the presence of the substring "world" within the string s
# The 'in' operator performs a search to determine if the sequence exists.
# A boolean True is returned if the sequence is identified.
print(f"Is \"world\" present in the string? { 'world' in s }\n")
# Evaluating the presence of the substring "xyz" within the string s
# A boolean False is returned if the sequence is not identified.
print(f"Is \"xyz\" present in the string? { 'xyz' in s }")

The in operator returns True if the substring is found and False otherwise.

Finding the position

When the exact position of the first occurrence is needed, Python provides the find() method, which returns the starting index of the substring. If the substring is not present, find() returns -1.

Python 3.14.0
s = "hello world"
# Retrieving the starting index of the first occurrence of "world"
# The find method returns the lowest index where the substring begins.
# If the sequence is identified, an integer index (6) is returned.
print(f"The starting index of \"world\" in the string: {s.find('world')}\n")
# Attempting to retrieve the starting index of the sequence "xyz"
# If the substring is not present within the string, the method returns -1.
print(f"The starting index of \"xyz\" (not present) in the string: {s.find('xyz')}")

Python also provides the index() method, which behaves identically to find() but raises a ValueError instead of returning -1 when the substring is not found.

Python 3.14.0
s = "hello world"
# Retrieving the starting index of the first occurrence of "world"
# The find method returns the lowest index where the substring begins.
# If the sequence is identified, an integer index (6) is returned.
print(f"The starting index of \"world\" in the string: {s.index("world")}\n")
# Attempting to retrieve the starting index of the sequence "xyz"
# If the substring is not present within the string, the method returns a ValueError: substring not found.
print(f"The starting index of \"xyz\" (not present) in the string: {s.index("xyz")}")

The choice between find() and index() depends on whether the absence of the ...