Skip to main content
Find and Replace First Occurrence Of a String in python

3 Ways to Find and Replace First Occurrence in Python

The find and replace is a very common operation in web development. Here, We’ll discuss different approaches to finding and replacing a substring’s first occurrence in a string using Python.

Well, discuss different ways to find and replace strings in Python with examples.

Replace Occurrences of Substrings in Strings in Python

The Python programming language includes a built-in ‘replace’ method, which is frequently utilized for locating and substituting strings within Python.

Here, We will discuss the following ways to replace occurrences of substrings in Strings in Python:

  • Using str.replace() method
  • Using find() method
  • Using regular expressions(re.sub())

Using replace() Method

The replace() method is designed to replace occurrences of a specified substring within a given string with another. By default, replaces() method finds and replaces all occurrences of a string. You can use concatenation and slicing to replace only the first occurrence.

The syntax is :

new_string = original_string.replace(old_substring, new_substring)

Whereas:

  • original_string: The string in which you want to replace occurrences.
  • old_substring: The substring you want to replace.
  • new_substring: The substring that will replace occurrences of old_substring.
  • count (optional): The number of occurrences to replace. If not provided, all occurrences are replaced.

Let’s create a simple code using Python, where we want to replace only the first occurrence of a substring within a string. The sample example is:

original_string = "Python is a powerful language!.Python Known for its clear syntax.I love python."
new_string = original_string.replace("Python", "PHP", 1)
print(new_string)

In the above code, We are replacing the first occurrence of “Python” with “PHP”. The count 1 in the replace() method indicates that only the first occurrence should be replaced.The output will be:

PHP is a powerful language!.Python Known for its clear syntax.I love python.

** Process exited - Return Code: 0 **
Press Enter to exit terminal

Note: The replace() method is case-sensitive. The method won’t affect occurrences of “python” in the above example.

Case-Insensitive Replacement

You can also achieve the same result in a case-insensitive manner by converting both the original string and the substrings to lowercase using the lower() method:

original_string = "python is a powerful language!.Python Known for its clear syntax.I love python."
substring_to_replace = "PYTHON".lower()
replacement = "Java"

original_string_lowercase = original_string.lower()
new_string = original_string_lowercase.replace(substring_to_replace, replacement, 1)
print(new_string)

Output:

PHP is a powerful language!.python known for its clear syntax.i love python.

** Process exited - Return Code: 0 **
Press Enter to exit terminal

Using find() method

The find() method is to locate the position of the first occurrence of the word, and then perform the replacement using slicing and concatenation.

The syntax is as follows:

str.find(substring, start, end)
  • str: The string in which you want to search for the substring.
  • substring: The substring you want to find in the string.
  • start (optional): The starting index for the search. If not specified, the search starts from the beginning of the string.
  • end (optional): The ending index for the search. If not specified, the search extends to the end of the string.

Example: Let’s replace the first occurrence of substring

original_string = "Python is a powerful language!.Python Known for its clear syntax.I love python."
start_index = original_string.find("powerful")
new_string = original_string[:start_index] + "strong" + original_string[start_index+5:]
print(new_string)

Output:

Python is a strongful language!.Python Known for its clear syntax.I love python.

** Process exited - Return Code: 0 **
Press Enter to exit terminal

Using Regular Expressions:

The sub() function from the re module in Python can be used to find and replace the first occurrence of a pattern in a string.

The syntax:

re.sub(pattern, repl, string, count=0, flags=0)
  • pattern: This is a regular expression that specifies the word or phrase to search for.
  • repl: This is the replacement word or phrase.
  • string: which needs to find in the string.
  • count: This is optional argument, and specifies the number of occurrences to replace.

Example: Replace First Occurrence Using Regular Expression

original_string = "Python is a powerful language!.Python Known for its clear syntax.I love python."
substring_to_replace = "PYTHON"
replacement = "Java"

new_string = re.sub(re.escape(substring_to_replace), replacement, original_string, count=1)
print(new_string)

The re.sub() function is then used with count=1 to replace only the first occurrence of the pattern with the replacement.

Output:

Python is a strongful language!.Python Known for its clear syntax.I love python.

** Process exited - Return Code: 0 **
Press Enter to exit terminal

Conclusions:

We’ve explored different techniques to locate and replace the first occurrence of a word or phrase within a string using Python. you can apply one of the methods to replace the initial occurrence of a string with in string. Every approach has advantages and disadvantages.

Leave a Reply

Your email address will not be published. Required fields are marked *