Python program for removing i-th character from a string

Here, we are going to learn about the various methods for removing i-th character from a string in Python?
By Shivang Yadav Last updated : March 01, 2024

Python programming language is a high-level and object-oriented programming language. Python is an easy to learn, powerful high-level programming language. It has a simple but effective approach to object-oriented programming.

Strings in Python are immutable means they cannot be changed once defined.

Remove ith character from the string in Python

We will take the string and an index as input from the user and print a string after removing an ith character from the string in Python.

Example:

Input: "includehelp" , i = 4
Output: "incldehelp"

Method 1: Using loop

In the method, we will simply traverse the string and copy all characters to a new string except the one at the entered index.

Algorithm

  • Loop for the string,
    • if i = enterIndex, -> continue
    • else, add element to extra_string
  • Print extra_string

Program to remove i-th character from string in Python

# Python code to demonstrate method 
# to remove i-th character

myStr =  input('Enter the string : ')
i = int(input('Enter the index of character to be removed : '))

resStr = ""

for index in range(len(myStr)):
	if index != i:
		resStr = resStr + myStr[index]

# Printing all strings... 
print ("Entered string : " + myStr)
print ("String formed by removing i'th character : " + resStr)

Output:

Enter the string : Hello world.
Enter the index of character to be removed : 3
Entered string : Hello world.
String formed by removing i'th character : Helo world.

Method 2: Using replace() method on string

We can replace all occurrences of an element in a string with any other character using the replace() method on the string.

Syntax

str.replace()

For the given index, we will find the character using str[i] notation.

Program to remove i-th character from string in python

# Python code to demonstrate method to 
# remove i-th character using replace() method 

# Taking string input from user
myStr =  input('Enter the string : ')
i = int(input('Enter the index of character to be removed : '))

# removing character at the specified index
resStr = myStr.replace(myStr[i], "", 1)

# Printing all strings... 
print ("Entered string : " + myStr)
print ("String formed by removing i'th character : " + resStr)

Output:

Enter the string : Hello world.
Enter the index of character to be removed : 3
Entered string : Hello world.
String formed by removing i'th character : Helo world.

Method 3: Using slice method with string concatenation

We can slice the string into parts and then concatenate it back after removing the element at a given index. We will slice using [:] operator and then concatenate it using the + operator.

Program to remove i-th character from string using concatenation

# Python code to demonstrate method to 
# remove i-th character using 
# slice and concatenation method 

# Taking string input from user
myStr =  input('Enter the string : ')
i = int(input('Enter the index of character to be removed : '))

# removing character at the specified index
resStr = myStr[:i] +  myStr[(i+1):]

# Printing all strings... 
print ("Entered string : " + myStr)
print ("String formed by removing i'th character : " + resStr)

Output:

Enter the string : Hello world.
Enter the index of character to be removed : 3
Entered string : Hello world.
String formed by removing i'th character : Helo world.

To understand the above programs, you should have the basic knowledge of the following Python topics:

Python String Programs »

Related Programs

Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.