私は何もあなたが持っている私は、文字列メソッド.replaceで
in> #first case
in>print('Hello, World!'.replace('l','L'))
out>HeLLo, WorLd!
#second case
a = 'Hello, World!'
a.replace('l','L')
print(a)
out>Hello, World!
私は何もあなたが持っている私は、文字列メソッド.replaceで
in> #first case
in>print('Hello, World!'.replace('l','L'))
out>HeLLo, WorLd!
#second case
a = 'Hello, World!'
a.replace('l','L')
print(a)
out>Hello, World!
を変更していない第二の場合には、文字列(場合、実際の文字)の一部を交換していたときに、なぜそれが起こるん混乱しています
a = 'Hello, World!'
a = a.replace('l','L')
print(a)
戻るsubstriのすべての出現で、文字列sのコピー変数に置き換えるの結果を代入します古いものは新しいものに置き換えられました。オプション引数maxreplaceが指定された場合、最初のmaxreplaceオカレンスが置き換えられます。
すでに述べたように、値を返す必要があります。
'replace'は既存の文字列を変更しないためです。新しいバージョンの文字列を作成して返します。 – JCVanHamme
Pythonの文字列は不変であることを理解することが重要です。 – polku
これを行うa.replace( 'l'、 'L')は文字列 'a'には影響しません.Totoによると、変数や変数に代入する必要があります。 – berthos