📌  相关文章
📜  用于从字符串中删除第 i 个字符的Python程序

📅  最后修改于: 2022-05-13 01:55:44.823000             🧑  作者: Mango

用于从字符串中删除第 i 个字符的Python程序

给定字符串,我们必须从字符串中删除第 i索引字符。

在任何字符串中,索引总是从 0 开始。假设我们有一个字符串geeks,那么它的索引将是 -

g e e k s
0 1 2 3 4

例子 :

Input : Geek
        i = 1
Output : Gek 

Input : Peter 
        i = 4
Output : Pete

方法 1:从给定的字符串中,必须删除第 i 个索引元素。因此,将字符串分成两半,在索引字符之前和索引字符之后。返回合并后的字符串。

以下是上述方法的实现:

# Python3 program for removing i-th 
# indexed character from a string
  
# Removes character at index i
def remove(string, i): 
  
    # Characters before the i-th indexed
    # is stored in a variable a
    a = string[ : i] 
      
    # Characters after the nth indexed
    # is stored in a variable b
    b = string[i + 1: ]
      
    # Returning string after removing
    # nth indexed character.
    return a + b
      
# Driver Code
if __name__ == '__main__':
      
    string = "geeksFORgeeks"
      
    # Remove nth index element
    i = 5
    
    # Print the new string
    print(remove(string, i))
输出:
geeksORgeeks

方法 2:想法是在Python中使用字符串替换

# Python3 program for removing i-th 
# indexed character from a string
  
# Removes character at index i
def remove(string, i): 
  
    for j in range(len(string)):
        if j == i:
            string = string.replace(string[i], "", 1)
    return string
      
# Driver Code
if __name__ == '__main__':
      
    string = "geeksFORgeeks"
      
    # Remove nth index element
    i = 5
    
    # Print the new string
    print(remove(string, i))
输出:
geeksORgeeks