Change language

Methods to remove i-th character from string in Python

|

Method 1: Naive Method

In this method, you just start a loop and add a character as it appears and build a new line from the existing one, unless the index is i .

Code # 1: Demonstration of the Naive method for removing the i-th character from a string.

# Demo Python code
# method to remove the i-th character
# Naive method

 
# Initializing String

test_str = "GeeksForGeeks"

 
# Print original line

print ( < code class = "string"> "The original string is:" + test_str)

 
# Deleting a character in pos. 3
# using a loop

new_str = " "

  

for i in range ( 0 , len (test_str)):

if i! = 2 :

new_str = new_str + test_str [i]

 
# П Printing lines after deletion

print ( " The string after removal of i’th character: " + new_str)

Exit:

 The original string is: GeeksForGeeks The string after removal of i’th character: GeksForGeeks 

Method 2: using str.replace()

replace () can be used to perform the removal task as we can replace a specific index with a blank character and hence fix the problem.

Disadvantage: The main disadvantage of this approach is that it does not work in the case of duplicates in the string that match the character in pos. I.  replace () replaces all occurrences of a particular character and therefore replaces all occurrences of all characters at position i. We can still sometimes use this function if the replacement character occurs 1 time in the line.

Code # 2: Demonstration of using str.replace () to remove the i-th character.

# Python code for demonstration
# removal method i -th character
# using replace ()

 
# Initializing String

test_str = "GeeksForGeeks"

 
# Print original strings

print ( "The original string is: " + test_str)

 
# Remove character in pos. 3
# using replacement

new_str = test_str.replace ( ’ e’ , ’’)

 
# Print line after deletion
# removes all occurrences of & # 39; e & # 39;

print ( "The string after removal of i’th character (doesn’t work):" + new_str)

 
# Removing the 1st occurrences of s, i.e. 5th pos.
# if we want to remove this.

new_str = test_str.replace ( ’s’ , ’’, 1 )

  
# Print line after deletion
# deletes first occurrences of s

print ( "The string after removal of i’th character (works): " + new_str)

Exit:

 The original string is: GeeksForGeeks The string after removal of i’ th character (doesn’t work): GksForGks The string after removal of i’th character (works): GeekForGeeks 

Method 3 : Using slice + concatenation

Once you can use a line slice and slice the line before position i, and the slice — after position i. Then, using string concatenation of both, the i-th character can be removed from the string.

Code # 3: Demonstrates using slice and concatenation to remove the i-th character.

# Python code for demonstration
# method for removing the i-th character
# using slice + concatenation

 
# Initializing String

test_str = "GeeksForGeeks "

  
# Print original line

print ( "The original string is:" + test_str)

 
# Deleting a character in pos ... 3
# using slice + concatenation

new_str = test_str [: 2 ] +   test_str [ 3 :]

 
# Print line after deletion
# removes the element at the 3rd index

print ( "The string after removal of i’th character:" + new_str)

Output:

 The original string is: GeeksForGeeks The string after removal of i’th character: GeksForGeeks 

Method 4: Using str.j oin () and list comprehension

In this method, each element in a string is first converted as every element in a list, and then each is concatenated into a string except for the specified index.

Code # 4: Demo str.join () and list comprehension to remove the i-th char index.

 

# Python code for demonstration
# method for removing the i-th character
# using join () + list comprehension

 
# Initializing String

test_str = "GeeksForGeeks"

 
# Print original line

print ( " The original string is: " + test_str)

 
# Deleting a symbol in pos. 3
# using join () + list comprehension

new_str = ’’ .join ([test_str [i] for i in range ( len (test_str)) if i ! = 2 ])

 
# Print line after deletion
# deletes element at 3rd index

print ( "The string after removal of i’th character:" + new_str)

Output:

 The original string is: GeeksForGeeks The string after removal of i ’th character: GeksForGeeks 

Shop

Learn programming in R: courses

$

Best Python online courses for 2022

$

Best laptop for Fortnite

$

Best laptop for Excel

$

Best laptop for Solidworks

$

Best laptop for Roblox

$

Best computer for crypto mining

$

Best laptop for Sims 4

$

Latest questions

NUMPYNUMPY

Common xlabel/ylabel for matplotlib subplots

12 answers

NUMPYNUMPY

How to specify multiple return types using type-hints

12 answers

NUMPYNUMPY

Why do I get "Pickle - EOFError: Ran out of input" reading an empty file?

12 answers

NUMPYNUMPY

Flake8: Ignore specific warning for entire file

12 answers

NUMPYNUMPY

glob exclude pattern

12 answers

NUMPYNUMPY

How to avoid HTTP error 429 (Too Many Requests) python

12 answers

NUMPYNUMPY

Python CSV error: line contains NULL byte

12 answers

NUMPYNUMPY

csv.Error: iterator should return strings, not bytes

12 answers

News


Wiki

Python | How to copy data from one Excel sheet to another

Common xlabel/ylabel for matplotlib subplots

Check if one list is a subset of another in Python

sin

How to specify multiple return types using type-hints

exp

Printing words vertically in Python

exp

Python Extract words from a given string

Cyclic redundancy check in Python

Finding mean, median, mode in Python without libraries

cos

Python add suffix / add prefix to strings in a list

Why do I get "Pickle - EOFError: Ran out of input" reading an empty file?

Python - Move item to the end of the list

Python - Print list vertically