Python で文字列から文字を削除する方法

文字列は、テキスト/文字を表すために使用されるデータ型です。この記事では、i を削除する問題に対するさまざまな方法を紹介します。 番目 文字列から文字を抽出し、Python を使用してそれらを実現するために採用できる解決策について説明します。

  Input:   'Geeks123For123Geeks'   Output:   GeeksForGeeks   Explanation:   In This, we have removed the '123' character from a string. 

Python で文字列から文字を削除する

これらは、文字列から文字を削除できる次のメソッドです。 パイソン :

  • 使用する str.replace()
  • 使用する 翻訳する()
  • 使用する 再帰
  • ネイティブメソッドの使用
  • スライス + 連結の使用
  • 使用する str.join()
  • 使用する バイト配列
  • 使用する プレフィックスを削除する ()

replace() を使用して文字列から文字を削除する

str.replace() を使用すると、目的の文字が出現するすべてを置き換えることができます。また、特定のインデックスを空の文字に置き換えて問題を解決できるため、文字列から文字を削除するタスクを実行するために使用することもできます。

Python3




# Initializing String> test_str> => 'GeeksForGeeks'> # Removing char at pos 3> # using replace> new_str> => test_str.replace(> 'e'> , '')> # Printing string after removal> # removes all occurrences of 'e'> print> (> 'The string after removal of i'th character( doesn't work) : '> +> new_str)> # Removing 1st occurrence of s, i.e 5th pos.> # if we wish to remove it.> new_str> => test_str.replace(> 's'> , '',> 1> )> # Printing string after removal> # removes first occurrences of s> print> (> 'The string after removal of i'th character(works) : '> +> new_str)>

出力

The string after removal of i'th character( doesn't work) : GksForGks The string after removal of i'th character(works) : GeekForGeeks 

時間計算量: の上)
空間の複雑さ: の上)

欠点: replace() を使用することの主な欠点は、文字列内に pos の char に一致する重複がある場合に失敗することです。私。 replace() は特定の文字のすべての出現を置換するため、位置 i にあるすべての文字の出現をすべて置き換えます。置換文字が 1 に発生する場合は、この関数をまだ使用できることがあります。 セント 文字列内の時間。

Translate() を使用して文字列から特定の文字を削除する

このメソッドは、文字列から文字を削除する強力なメカニズムを提供します。この方法では、次を使用して techcodeview.com から 123 を削除しました。 string.translate()

Python3




str> => 'Geeks123For123Geeks'> > print> (> str> .translate({> ord> (i):> None> for> i> in> '123'> }))>

出力

GeeksForGeeks 

時間計算量: の上)
空間の複雑さ: オ(メートル)

再帰を使用して文字列から特定の文字を削除する

再帰を使用して文字列から i 番目の文字を削除するには、削除する文字列とインデックスを引数として受け取る再帰関数を定義できます。この関数はインデックスが 0 に等しいかどうかをチェックし、この場合は最初の文字が削除された文字列を返します。インデックスが 0 でない場合、関数は、インデックスが 1 減分された文字列に対して関数を再度呼び出した結果と連結された文字列の最初の文字を返すことができます。

Python3




def> remove_ith_character(s, i):> > # Base case: if index is 0,> > # return string with first character removed> > if> i> => => 0> :> > return> s[> 1> :]> > # Recursive case: return first character> > # concatenated with result of calling function> > # on string with index decremented by 1> > return> s[> 0> ]> +> remove_ith_character(s[> 1> :], i> -> 1> )> # Test the function> test_str> => 'GeeksForGeeks'> new_str> => remove_ith_character(test_str,> 2> )> print> (> 'The string after removal of ith character:'> , new_str)> # This code is contributed by Edula Vinay Kumar Reddy>

出力

The string after removal of ith character: GeksForGeeks 

時間計算量: の上)
空間の複雑さ: の上)

ネイティブ メソッドを使用して文字列から文字を削除する

この方法では、次のコマンドを実行するだけです。 Python ループ 文字が来るたびに追加し、インデックスが i の場合を除いて、既存の文字列から新しい文字列を構築します。

Python3




test_str> => 'GeeksForGeeks'> # Removing char at pos 3> new_str> => ''> for> i> in> range> (> len> (test_str)):> > if> i !> => 2> :> > new_str> => new_str> +> test_str[i]> # Printing string after removal> print> (> 'The string after removal of i'th character : '> +> new_str)>

出力

The string after removal of i'th character : GeksForGeeks 

時間計算量: の上)
空間の複雑さ: O(n)、n は文字列の長さです。

「i」を削除します 番目 スライスを使用した文字列からの文字

使用できます 文字列スライス そして、位置 i の前の文字列をスライスし、位置 i の後ろで文字列をスライスします。次に、使用 文字列の連結 両方とも、私は 番目 文字が文字列から削除されたように見える場合があります。

Python3




# Initializing String> test_str> => 'GeeksForGeeks'> # Removing char at pos 3> # using slice + concatenation> new_str> => test_str[:> 2> ]> +> test_str[> 3> :]> # Printing string after removal> # removes ele. at 3rd index> print> (> 'The string after removal of i'th character : '> +> new_str)>

出力

The string after removal of i'th character : GeksForGeeks 

時間計算量: の上)
空間の複雑さ: の上)

「i」を削除します 番目 文字列からの文字の使用 str.join()

このメソッドでは、文字列の各要素がまずリストの各要素として変換され、その後、指定されたインデックスを除いてそれぞれが結合されて文字列を形成します。

Python3




# Initializing String> test_str> => 'GeeksForGeeks'> # Removing char at pos 3> # using join() + list comprehension> new_str> => ''.join([test_str[i]> for> i> in> range> (> len> (test_str))> if> i !> => 2> ])> # Printing string after removal> # removes ele. at 3rd index> print> (> 'The string after removal of i'th character : '> +> new_str)>

出力

The string after removal of i'th character : GeksForGeeks 

時間計算量: の上)
空間の複雑さ: の上)

bytearray を使用して Python で文字列から文字を削除する

文字列 s と整数 i を入力として受け取る関数 Remove_char(s, i) を定義します。次に、 bytearray(s, ‘utf-8’) を使用して入力文字列 s を bytearray に変換します。 i 番目の要素を削除します。 バイト配列 del b[i]を使用します。 b.decode() を使用して、変更された bytearray を文字列に変換し、変更された文字列を返します。

Python3




def> remove_char(s, i):> > b> => bytearray(s,> 'utf-8'> )> > del> b[i]> > return> b.decode()> # Example usage> s> => 'hello world'> i> => 4> s> => remove_char(s, i)> print> (s)>

出力

hell world 

時間計算量: の上)
空間の複雑さ: の上)

Removeprefix() を使用して文字列から文字を削除する

削除プレフィックス() プレフィックスを削除し、文字列の残りの部分を返します。特定のインデックスの文字列から文字を削除するには、削除したい文字が 2 つのパーティションのいずれかのプレフィックスに含まれるように文字列を 2 つに分割し、その文字を削除するメソッドを適用します。

Python3




#initializing the string> s> => 'techcodeview.com'> #if you wanted to remove 'G' of 0th index> s1> => s.removeprefix(> 'G'> )> #if you wanted to remove 'f'> s2> => s[:> 5> ]> +> s[> 5> :].removeprefix(> 'f'> )> print> (s1)> print> (s2)>

出力:

eeksforGeeks GeeksorGeeks 

時間計算量: の上)
空間の複雑さ: の上)