Python 3.xでstring.replace()を使用する方法


279

string.replace()はpython 3.xでは非推奨です。これを行う新しい方法は何ですか?


11
FWIW、私も同じように混乱しました。Googleの「python string replace」を使用すると、Python 2.7で廃止された古い文字列関数に移動しました。私見、そのセクションでは、「太字のボックス」を使用して「string.xxx()」と「xxx(string)」を説明し、非推奨の文字列メソッド、たとえばdocs.python.org/library/stdtypes.htmlにユーザー
ToolmakerSteve

8
Pythonのドキュメントは、理想的な第一言語として宣伝されていることを考えると、絶対的な修羅場です。非常に頻繁にコンテンツが存在しますが、その整理方法が不十分なため、検索エンジンや独自のサイトでさえインデックス付けされていないことがよくあります。ToolMakerSteveのリンクを見てください。コア文字列関数は標準型にまとめられています。これは、文字列関数を検索するときには表示されません。
Andrew S

10
明確にするには、次のstring.replace()は実際にはPython 3に廃止予定されていません
sboggs11


1
[文字列変数] .replaceを意味する場合、 "str.replace"と書くことがあります。'str'は関連するクラスの名前でもあるため、混乱を招く可能性があります。
TextGeek

回答:


315

2.xと同様に、を使用しますstr.replace()

例:

>>> 'Hello world'.replace('world', 'Guido')
'Hello Guido'

4
"re"(正規表現)モジュールには、(一部?すべて?)非推奨の文字列関数の代替手段があります。この場合は、re.sub()
ToolmakerSteve

8
@ToolmakerSteve:string関数は非推奨です。strメソッドはそうではありません。
Ignacio Vazquez-Abrams

6
FWIW、グーグルを回すたびに、古い非推奨の文字列関数になってしまうようです。これは(非推奨ではない)文字列メソッドへのリンクです。docs.python.org/3.3/library/stdtypes.html#string-methods 〜or_for_2〜 docs.python.org/2/library/stdtypes.html#string-methods
ToolmakerSteve

36
Pythonドキュメントをナビゲートするためにスタックオーバーフローに来なければならない場合、明らかに問題があります。あなたが読んでいるならPythonチーム。それを整理する!
Andrew S

2
クラスではなくオブジェクトのメソッドを呼び出します。'foo'.replace(...)
Ignacio Vazquez-Abrams

108

replace()<class 'str'>python3のメソッドです:

>>> 'hello, world'.replace(',', ':')
'hello: world'

13

Python 3のreplace()メソッドは、次のように使用されます。

a = "This is the island of istanbul"
print (a.replace("is" , "was" , 3))

#3 is the maximum replacement that can be done in the string#

>>> Thwas was the wasland of istanbul

# Last substring 'is' in istanbul is not replaced by was because maximum of 3 has already been reached

2
また、3を置くことはできず、すべての偶然が変わることにも注意してください。
Ender Look

4

あなたが使用することができますstr.replaceを()などのチェーンstr.replace() 。次のような文字列が'Testing PRI/Sec (#434242332;PP:432:133423846,335)'あり、すべての'#',':',';','/'記号をに置き換えたいと考えます'-'。この方法(通常の方法)で交換できます。

>>> str = 'Testing PRI/Sec (#434242332;PP:432:133423846,335)'
>>> str = str.replace('#', '-')
>>> str = str.replace(':', '-')
>>> str = str.replace(';', '-')
>>> str = str.replace('/', '-')
>>> str
'Testing PRI-Sec (-434242332-PP-432-133423846,335)'

またはこのように(str.replace()のチェーン)

>>> str = 'Testing PRI/Sec (#434242332;PP:432:133423846,335)'.replace('#', '-').replace(':', '-').replace(';', '-').replace('/', '-')
>>> str
'Testing PRI-Sec (-434242332-PP-432-133423846,335)'

3

これを試して:

mystring = "This Is A String"
print(mystring.replace("String","Text"))

1

参考までに、文字列内の任意の位置固定された単語に一部の文字を追加する場合(例:接尾辞-lyを追加して形容詞を副詞に変更)、読みやすくするために行の末尾に接尾辞を付けることができます。これを行うには、split()insideを使用しますreplace()

s="The dog is large small"
ss=s.replace(s.split()[3],s.split()[3]+'ly')
ss
'The dog is largely small'

0
ss = s.replace(s.split()[1], +s.split()[1] + 'gy')
# should have no plus after the comma --i.e.,
ss = s.replace(s.split()[1], s.split()[1] + 'gy')

6
このコードは質問に回答する場合がありますが、なぜまたはどのように質問に回答するかに関する追加のコンテキストを提供すると、長期的な価値が大幅に向上します。回答を編集して、説明を追加してください。
CodeMouse92 2016年

正解は以前に述べたstring.replaceはpython3で動作します。
ジョルフ
弊社のサイトを使用することにより、あなたは弊社のクッキーポリシーおよびプライバシーポリシーを読み、理解したものとみなされます。
Licensed under cc by-sa 3.0 with attribution required.