Introduction:

Strings in Python come with powerful built-in methods that allow us to easily search, manipulate, and format text. These operations are essential for many programming tasks, such as cleaning up messy data, searching for specific words, or altering text to suit your needs. Understanding how to find, replace, split, and slice strings will give you the ability to handle almost any text-related problem in Python.

Here’s a breakdown of the string methods we'll cover:

  • .find() – This method is used to locate a substring within a string. It returns the position (or index) of the first occurrence of the substring. If the substring isn't found, it returns -1.
  • .replace() – This method allows you to replace parts of a string with another string. It’s particularly useful for updating or modifying text.
  • .split() – This method breaks a string into a list of words, separating them by spaces or other specified delimiters.
  • .strip() – This method removes any leading or trailing whitespace from a string. It’s helpful for cleaning up user input.
  • mid(), left(), right() – These can be simulated using Python’s slicing. left() extracts the first part of a string, right() extracts the last part, and mid() extracts a portion from the middle.

Let’s look at examples of how each of these works.

.find() and .replace()

Here’s how you can use .find() to locate a word in a sentence and .replace() to change part of a string:

In this example:

  • .find('amazing') will return the position of the word "amazing" within the string.
  • .replace('amazing', 'fun') will replace "amazing" with "fun" in the string.

.split() and .strip()

You often need to clean up strings or break them into smaller pieces. .split() helps by splitting a string into a list of words, while .strip() removes extra spaces from the start and end of the string.

In this example:

  • .strip() removes the extra spaces before and after the sentence.
  • .split() turns the sentence into a list of words by splitting at the spaces.

left(), mid(), and right() (Simulated with Slicing)

Python doesn't have specific left(), mid(), and right() functions like some other languages, but we can easily simulate this behaviour using slicing. Let’s see how to extract parts of a string:

In this example:

  • text[:6] extracts the first 6 characters, which simulates a left() function.
  • text[7:9] extracts a middle part of the string, similar to mid().
  • text[-9:] extracts the last 9 characters, simulating a right() function.

Predict:

Can you predict the output of this code?

message = "I love programming in Python"
position = message.find("Python")
new_message = message.replace("Python", "Java")
print(position)
print(new_message)

Study the code and think about what .find() and .replace() will do. What will be the position of "Python"? What will the new message say?

Run:

Run the following code to see how .split() and .strip() work. You’ll see how split() breaks the string into parts and how strip() cleans up unwanted spaces.

Investigate:

Now, let’s explore Python's slicing capabilities for mid(), left(), and right(). These can be adapted from slicing techniques in Python. Run the following code and observe how we can extract different parts of the string.

Instructions:

  • Notice how Python slicing is used to simulate the functionality of left(), mid(), and right().
  • Experiment by changing the start and end indices to extract different parts of the string.

Modify:

The following code attempts to replace a word in the sentence and find a substring, but there’s a small mistake in how the replacement is done. Can you correct it so the program works as expected?

Hint: Make sure the word "fun" is fully replaced in the sentence. There might be missing words in the replacement string.

Scroll to Top