Latest web development tutorials

Python metodo replace ()

stringhe Python stringhe Python


descrizione

Python sostituire () per una serie di vecchi (vecchio stringa) sostituito nuova (nuova stringa), se si specifica il terzo parametro massimo, poi sostituire non più di volte max.

grammatica

sostituire il metodo di sintassi ():

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

parametri

  • vecchio - sarà sostituito con la stringa.
  • new - nuova stringa, che sostituisce vecchia stringa.
  • max - stringa opzionale per sostituire non più di volte max

Valore di ritorno

Restituisce una stringa di vecchio (vecchio stringa) sostituito con una nuova stringa nuova (new String) dopo generazione, se si specifica il terzo parametro massimo, quindi sostituire non più di volte max.

Esempi

L'esempio seguente mostra il metodo replace () utilizzando la funzione di:

#!/usr/bin/python

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

Esempi dei risultati di output di cui sopra sono i seguenti:

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

stringhe Python stringhe Python