Metodo Python String center ()

Metodo delle stringhe Python center()restituisce centrato in una stringa di lunghezza larghezza. Il riempimento viene eseguito utilizzando il fillchar specificato. Il riempimento predefinito è uno spazio.

Sintassi

str.center(width[, fillchar])

Parametri

  • width - Questa è la larghezza totale della stringa.

  • fillchar - Questo è il carattere di riempimento.

Valore di ritorno

Questo metodo restituisce centrato in una stringa di lunghezza larghezza.

Esempio

L'esempio seguente mostra l'utilizzo del metodo center ().

#!/usr/bin/python

str = "this is string example....wow!!!"
print "str.center(40, 'a') : ", str.center(40, 'a')

Risultato

str.center(40, 'a') :  aaaathis is string example....wow!!!aaaa