Il simbolo % viene utilizzato in Python con un'ampia varietà di tipi di dati e configurazioni. %s viene utilizzato specificamente per eseguire la concatenazione di stringhe insieme. Ci permette di formattare un valore all'interno di una stringa. Viene utilizzato per incorporare un'altra stringa all'interno di una stringa. Fornisce automaticamente la conversione del tipo da valore a stringa.
cos'è il computer
L'operatore %s viene inserito nel punto in cui deve essere specificata la stringa. Il numero di valori che desideri aggiungere a una stringa deve essere equivalente al numero specificato tra parentesi dopo l'operatore % alla fine del valore della stringa.
Il seguente codice Python illustra il modo di eseguire la formattazione delle stringhe.
Utilizzo semplice di %s
Python3
# declaring a string variable> name>=> 'Geek'> # append a string within a string> print>(>'Hey, %s!'> %> name)> |
>
>
Produzione
Hey, Geek!>
Più %s
È inoltre possibile aggiungere più stringhe all'interno di una singola stringa utilizzando l'operatore %s. Le stringhe vengono sostituite nell'ordine della loro posizione tra parentesi, ovunque sia presente un segno %s. Ciò è illustrato utilizzando il seguente frammento di codice:
Python3
# declaring a string variable> var1>=> 'Geek!'> var2>=> 'Geeks for Geeks'> # append multiple strings within a string> print>(>'Hello %s Are you enjoying being at %s for preparations.'> %> (var1, var2))> |
>
>
Produzione
Ciao Geek! Ti piace stare a Geeks for Geeks per i preparativi.
Mappatura delle stringhe su %s
Tuttavia, il numero di occorrenze di questo operatore deve essere uguale al numero di stringhe da sostituire dopo il segno %. In caso contrario, verrà generato un errore del tipo TypeError: argomenti insufficienti per la stringa di formato.
Python3
ariano khan
# declaring string variables> str1>=> 'Understanding'> str2>=> '%s'> str3>=> 'at'> str4>=> 'techcodeview.com'> # concatenating strings but %s not equal to string variables> final_str>=> '%s %s %s %s'> %> (str1, str3, str4)> # printing the final string> print>(>'Concatenating multiple strings using Python '%s' operator:
'>)> print>(final_str)> |
>
>
Errore
Traceback (ultima chiamata per ultima):
File /home/c7b65fabd2ad00163eba70bbc39685d3.py, riga 8, in
final_str = %s %s %s %s % (str1, str3, str4)
TypeError: argomenti insufficienti per la stringa di formato
Codice corretto
Python3
# declaring string variables> str1>=> 'Understanding'> str2>=> '%s'> str3>=> 'at'> str4>=> 'techcodeview.com'> # concatenating strings> final_str>=> '%s %s %s %s'> %> (str1, str2, str3, str4)> # printing the final string> print>(>'Concatenating multiple strings using Python '%s' operator:
'>)> print>(final_str)> |
>
>
Produzione
Concatenating multiple strings using Python '%s' operator: Understanding %s at techcodeview.com>
Ordina %s utilizzando il dizionario
Le stringhe vengono stampate nell'ordine in cui vengono aggiunte utilizzando la chiave del dizionario nell'output.
Python3
kajal aggarwal
Java aggiunge all'array
# declaring string variables with dictionary> dct>=> {>'str1'>:>'at'>,> >'str2'>:>'techcodeview.com'>,> >'str3'>:>'Understanding'>,> >'str4'>:>'%s'>}> # concatenating strings> final_str>=> '%(str3)s %(str4)s %(str1)s %(str2)s'> %> dct> # printing the final string> print>(>'Concatenating multiple strings using Python '%s' operator:
'>)> print>(final_str)> |
>
>
Produzione
Concatenating multiple strings using Python '%s' operator: Understanding %s at techcodeview.com>
Elenca come a stringa per %s
Un operatore non stringa può anche essere formattato utilizzando il simbolo %s in Python. Le tuple possono anche essere inserite e formattate utilizzando questo operatore.
Python3
# declaring string variables> str1>=> 'Understanding'> str2>=> 'integers'> str3>=> 'at'> str4>=> 'techcodeview.com = '> # declaring list variables> lst>=> [>1>,>2>,>3>]> # concatenating strings as well as list> final_str>=> '%s %s %s %s %s'> %> (str1, str2, str3, str4, lst)> # printing the final string> print>(>'Concatenating multiple values using Python '%s' operator:
'>)> print>(final_str)> |
>
>
Produzione
Concatenating multiple values using Python '%s' operator: Understanding integers at techcodeview.com = [1, 2, 3]>