Latest web development tutorials

Python translate () metodo

stringhe Python stringhe Python


descrizione

Python tradurre () tabella dei parametri metodo indicato nella tabella (256 caratteri) carattere di conversione di stringa, per filtrare il carattere nel parametro del.

grammatica

tradurre il metodo sintassi ():

str.translate(table[, deletechars]);

parametri

  • Tavolo - Tavolo traduzione è convertito dalla tabella di traduzione con metodi maketrans.
  • deletechars - l'elenco dei caratteri nella stringa che si desidera filtrare.

Valore di ritorno

Restituisce una stringa tradotta.

Esempi

L'esempio seguente mostra tradurre () per usare:

#!/usr/bin/python

from string import maketrans   # 引用 maketrans 函数。

intab = "aeiou"
outtab = "12345"
trantab = maketrans(intab, outtab)

str = "this is string example....wow!!!";
print str.translate(trantab);

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

th3s 3s str3ng 2x1mpl2....w4w!!!

Esempi di quanto sopra per rimuovere la stringa 'x' e caratteri 'm':

#!/usr/bin/python

from string import maketrans   # Required to call maketrans function.

intab = "aeiou"
outtab = "12345"
trantab = maketrans(intab, outtab)

str = "this is string example....wow!!!";
print str.translate(trantab, 'xm');

Esempi di uscita sopra:

th3s 3s str3ng 21pl2....w4w!!!

stringhe Python stringhe Python