Esercitazione Python

Python HOME Introduzione di Python Python per iniziare Sintassi Python Commenti Python Variabili Python Tipi di dati Python Numeri Python Fusione in pitone Stringhe Python Python booleani Operatori Python Elenchi Python Tuple di Python Insiemi Python Dizionari Python Python Se... Altro Python While Loops Python per loop Funzioni Python Python Lambda Matrici Python Classi/oggetti Python Ereditarietà Python Iteratori Python Ambito Python Moduli Python Date Python Python matematica Python JSON Python RegEx PIP Python Python Prova... Tranne Input utente Python Formattazione di stringhe Python

Gestione dei file

Gestione dei file Python File di lettura Python Python Scrivi/Crea file Python Elimina file

Moduli Python

Tutorial NumPy Procedura dettagliata di Panda Tutorial Scipy

Python Matplotlib

Introduzione a Matplotlib Matplotlib Inizia Plottaggio Matplotlib Tracciatura Matplotlib Marcatori Matplotlib Linea Matplotlib Etichette Matplotlib Griglia Matplotlib Sottotrame Matplotlib Dispersione Matplotlib Barre Matplotlib Istogrammi Matplotlib Grafici a torta Matplotlib

Apprendimento automatico

Iniziare Modalità mediana media Deviazione standard percentile Distribuzione dei dati Distribuzione normale dei dati Trama a dispersione Regressione lineare Regressione polinomiale Regressione multipla Scala Treno/prova Albero decisionale

Python MySQL

MySQL per iniziare MySQL crea database MySQL Crea tabella Inserimento MySQL MySQL Seleziona MySQL dove MySQL Ordina per Elimina MySQL MySQL Drop Table Aggiornamento MySQL Limite MySQL Unisciti a MySQL

Python MongoDB

MongoDB Inizia MongoDB Crea database MongoDB Crea raccolta Inserisci MongoDB MongoDB Trova Interrogazione MongoDB Ordinamento MongoDB Elimina MongoDB Collezione Drop MongoDB Aggiornamento MongoDB Limite MongoDB

Riferimento Python

Panoramica di Python Funzioni integrate in Python Metodi di stringa Python Metodi dell'elenco Python Metodi del dizionario Python Metodi della tupla Python Metodi di impostazione Python Metodi di file Python Parole chiave Python Eccezioni Python Glossario Python

Riferimento del modulo

Modulo casuale Modulo Richieste Modulo Statistiche Modulo di matematica Modulo cMath

Python come fare per

Rimuovi i duplicati dell'elenco Invertire una stringa Aggiungi due numeri

Esempi Python

Esempi Python Compilatore Python Esercizi di Python Python Quiz Certificato Python

Python String format() Metodo

❮ Metodi di stringa


Esempio

Inserisci il prezzo all'interno del segnaposto, il prezzo deve essere a virgola fissa, formato due decimali:

txt = "For only {price:.2f} dollars!"
print(txt.format(price = 49))

Definizione e utilizzo

Il format()metodo formatta i valori specificati e li inserisce all'interno del segnaposto della stringa.

Il segnaposto viene definito utilizzando parentesi graffe: {}. Maggiori informazioni sui segnaposto nella sezione Segnaposto di seguito.

Il format()metodo restituisce la stringa formattata.


Sintassi

string.format(value1, value2...)

Valori dei parametri

Parameter Description
value1, value2... Required. One or more values that should be formatted and inserted in the string.

The values are either a list of values separated by commas, a key=value list, or a combination of both.

The values can be of any data type.

I segnaposto

I segnaposto possono essere identificati utilizzando indici con nome {price}, indici numerati {0}o anche segnaposto vuoti {}.

Esempio

Utilizzo di valori segnaposto diversi:

txt1 = "My name is {fname}, I'm {age}".format(fname = "John", age = 36)
txt2 = "My name is {0}, I'm {1}".format("John",36)
txt3 = "My name is {}, I'm {}".format("John",36)

Tipi di formattazione

All'interno dei segnaposto puoi aggiungere un tipo di formattazione per formattare il risultato:

:< Left aligns the result (within the available space)
:> Right aligns the result (within the available space)
:^ Center aligns the result (within the available space)
:= Places the sign to the left most position
:+ Use a plus sign to indicate if the result is positive or negative
:- Use a minus sign for negative values only
Use a space to insert an extra space before positive numbers (and a minus sign before negative numbers)
:, Use a comma as a thousand separator
:_ Use a underscore as a thousand separator
:b Binary format
:c Converts the value into the corresponding unicode character
:d Decimal format
:e Scientific format, with a lower case e
:E Scientific format, with an upper case E
:f Fix point number format
:F Fix point number format, in uppercase format (show inf and nan as INF and NAN)
:g General format
:G General format (using a upper case E for scientific notations)
:o Octal format
:x Hex format, lower case
:X Hex format, upper case
:n Number format
:% Percentage format

❮ Metodi di stringa