Q:

Python program for removing i-th character from a string

0

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"

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

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.

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now