Latest web development tutorials

Pythonのreplace()メソッド

Python文字列 Python文字列


説明

新しい(新しい文字列)に置き換えられますが、その後、第3のパラメータの最大を指定する最大回よりも多くを交換しない場合はPythonは、古い(旧文字列)の文字列に()メソッドを置き換えます。

文法

()メソッドの構文を置き換えます。

str.replace(old, new[, max])

パラメータ

  • 古い - サブストリングに置き換えられます。
  • 新しい - 古い部分を置き換える新しい文字列、。
  • 最大 - 最大回よりも多くを交換しないためのオプションの文字列

戻り値

あなたは三番目のパラメータmaxを指定した場合、生成後に新たな新しい文字列(新しいString)に置き換えられ、古いの文字列(旧文字列)を返し、その後、最大倍以下で交換していません。

次の例では、関数を使用してreplace()メソッドを示しています。

#!/usr/bin/python

str = "this is string example....wow!!! this is really string";
print str.replace("is", "was");
print str.replace("is", "was", 3);

次のように上記の出力結果の例は次のとおりです。

thwas was string example....wow!!! thwas was really string
thwas was string example....wow!!! thwas is really string

Python文字列 Python文字列