logo

Co znamená %s v řetězci formátu Pythonu?

Symbol % se používá v Pythonu s velkým množstvím datových typů a konfigurací. %s se konkrétně používá k provádění zřetězení řetězců dohromady. Umožňuje nám formátovat hodnotu uvnitř řetězce. Používá se k začlenění dalšího řetězce do řetězce. Automaticky poskytuje převod typu z hodnoty na řetězec.

Operátor %s je umístěn tam, kde má být řetězec specifikován. Počet hodnot, které chcete připojit k řetězci, by měl být ekvivalentní číslu uvedenému v závorkách za operátorem % na konci hodnoty řetězce.

Následující kód Pythonu ilustruje způsob provádění formátování řetězce.



Jednoduché použití %s

Python3




# declaring a string variable> name>=> 'Geek'> # append a string within a string> print>(>'Hey, %s!'> %> name)>

>

>

Výstup

Hey, Geek!>

Více %s

Více řetězců lze také připojit do jednoho řetězce pomocí operátoru %s. Řetězce jsou nahrazeny v pořadí jejich pozice v závorkách, kdekoli je znak %s. To je ilustrováno pomocí následujícího fragmentu kódu:

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))>

>

>

Výstup

Ahoj Geeku! Baví vás příprava v Geeks for Geeks?

Mapování řetězců na %s

Počet výskytů tohoto operátoru se však musí rovnat počtu řetězců, které mají být nahrazeny za znakem %. V opačném případě je vyvolána chyba typu TypeError: není dostatek argumentů pro formátovací řetězec.

Python3




# 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)>

>

>

Chyba

Traceback (poslední poslední hovor):

Soubor /home/c7b65fabd2ad00163eba70bbc39685d3.py, řádek 8, v

final_str = %s %s %s %s % (str1, str3, str4)

TypeError: nedostatek argumentů pro formátovací řetězec

Správný kód

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)>

>

>

Výstup

Concatenating multiple strings using Python '%s' operator: Understanding %s at techcodeview.com>

Objednejte %s pomocí slovníku

Řetězce se tisknou v libovolném pořadí, ve kterém jsou připojeny pomocí klíče slovníku ve výstupu.

Python3




# 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)>

>

fronta a prioritní fronta v Javě

>

Výstup

Concatenating multiple strings using Python '%s' operator: Understanding %s at techcodeview.com>

Seznam jako a řetězec pro %s

Neřetězcový operátor lze také naformátovat pomocí symbolu %s v Pythonu. Pomocí tohoto operátoru lze také vkládat a formátovat n-tice.

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)>

>

>

Výstup

Concatenating multiple values using Python '%s' operator: Understanding integers at techcodeview.com = [1, 2, 3]>