Python Program to Remove the First Occurrence of Character in a String

In this article, we’ll learn how to use Python to remove the first occurrence of a character in a string. The aim is to remove the first occurrence of a character in a string. To begin, locate the character in the string and confirm that it is the first occurrence. Finally, remove the character from the string and display the result.

Examples:

Example1:

Input:

Given String =goodmorningthisisbtechgeekspython
Given Character =s

Output:

The Given string after removing first occurence of the character [ s ] is goodmorningthisisbtechgeekspython
The Given string after removing first occurence of the character [ s ] is goodmorningthiisbtechgeekspython

Example2:

Input:

Given String =hellothisisbtechgeeks
Given Character =e

Output:

The Given string after removing first occurence of the character [ e ] is hellothisisbtechgeeks
The Given string after removing first occurence of the character [ e ] is hllothisisbtechgeeks

Program to Remove the First Occurrence of Character in a String in Python

Below are the ways to remove the first occurrence of the given character in a string in Python.

Practice Java programming from home without using any fancy software just by tapping on this Simple Java Programs for Beginners tutorial.

Method #1: Using For Loop (Static Input)

Approach:

  • Give the string as static input and store it in a variable.
  • Give the character as static input and store it in another variable.
  • Calculate the length of the string using the len() function.
  • Loop in the characters of the string using the For loop.
  • Check if the iterator character is equal to the given character using the if conditional statement and == operator.
  • Initialize the resultstrng with the given string from 0 to iterator value concatenated with iterator value +1 to the length of the given string.
  • Break the For loop using the break keyword.
  • Print the Result.
  • The Exit of the Program.

Below is the implementation:

# Give the string as static input and store it in a variable.
gvnstrng = 'hellothisisbtechgeeks'
# Give the character as static input and store it in another variable.
gvnchar = 'e'
# Calculate the length of the string using the len() function.
strnglength = len(gvnstrng)
# Loop in the characters of the string using the For loop.
for i in range(strnglength):
  # Check if the iterator character is equal to the given character
  # using the if conditional statement and == operator.
    if(gvnstrng[i] == gvnchar):
      # Initialize the resultstrng with the given string
      # from 0 to iterator value concatenated
      # with iterator value +1 to the length of the given string.
        reesultvalu = gvnstrng[0:i] + gvnstrng[i + 1:strnglength]
        # Break the For loop using the break keyword.
        break
# Print the Result.
print(
    'The Given string after removing first occurence of the character [', gvnchar, '] is', gvnstrng)
print(
    'The Given string after removing first occurence of the character [', gvnchar, '] is', reesultvalu)

Output:

The Given string after removing first occurence of the character [ e ] is hellothisisbtechgeeks
The Given string after removing first occurence of the character [ e ] is hllothisisbtechgeeks

Method #2: Using For Loop (User Input)

Approach:

  • Give the string as user input using the input() function and store it in a variable.
  • Give the character as user input using the input() function and store it in another variable.
  • Calculate the length of the string using the len() function.
  • Loop in the characters of the string using the For loop.
  • Check if the iterator character is equal to the given character using the if conditional statement and == operator.
  • Initialize the resultstrng with the given string from 0 to iterator value concatenated with iterator value +1 to the length of the given string.
  • Break the For loop using the break keyword.
  • Print the Result.
  • The Exit of the Program.

Below is the implementation:

# Give the string as user input using the input() function and store it in a variable.
gvnstrng = input('Enter some random given string = ')
# Give the character as user input using the input() function and store it in another variable.
gvnchar = input('Enter some random character = ')
# Calculate the length of the string using the len() function.
strnglength = len(gvnstrng)
# Loop in the characters of the string using the For loop.
for i in range(strnglength):
  # Check if the iterator character is equal to the given character
  # using the if conditional statement and == operator.
    if(gvnstrng[i] == gvnchar):
      # Initialize the resultstrng with the given string
      # from 0 to iterator value concatenated
      # with iterator value +1 to the length of the given string.
        reesultvalu = gvnstrng[0:i] + gvnstrng[i + 1:strnglength]
        # Break the For loop using the break keyword.
        break
# Print the Result.
print(
    'The Given string after removing first occurence of the character [', gvnchar, '] is', gvnstrng)
print(
    'The Given string after removing first occurence of the character [', gvnchar, '] is', reesultvalu)

Output:

Enter some random given string = goodmorningthisisbtechgeekspython
Enter some random character = s
The Given string after removing first occurence of the character [ s ] is goodmorningthisisbtechgeekspython
The Given string after removing first occurence of the character [ s ] is goodmorningthiisbtechgeekspython

Related Programs: