ADVERTISEMENT
ADVERTISEMENT

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?
Submitted by Shivang Yadav, on April 15, 2021

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.

Python String Programs »



ADVERTISEMENT
ADVERTISEMENT


Comments and Discussions!



ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT

Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.