Python String center ()

Il metodo center () restituisce una stringa che viene riempita con il carattere specificato.

La sintassi del center()metodo è:

 string.center (width (, fillchar))

center () Parametri

Il center()metodo accetta due argomenti:

  • larghezza - lunghezza della stringa con caratteri riempiti
  • fillchar (opzionale) - carattere di riempimento

L'argomento fillchar è facoltativo. Se non viene fornito, lo spazio viene preso come argomento predefinito.

Valore restituito dal centro ()

Il center()metodo restituisce una stringa riempita con fillchar specificato. Non modifica la stringa originale.

Esempio 1: metodo center () con fillchar predefinito

 string = "Python is awesome" new_string = string.center(24) print("Centered String: ", new_string)

Produzione

 Stringa centrata: Python è fantastico 

Esempio 2: metodo center () con * fillchar

 string = "Python is awesome" new_string = string.center(24, '*') print("Centered String: ", new_string)

Produzione

 Stringa centrata: *** Python è fantastico ****

Articoli interessanti...