71 of 151 menu

The format method

The format method substitutes data using string formatting. In the method parameter, we set the value we need, which will fill the empty curly brackets in the string.

Syntax

string.format(value)

Example

Let's substitute a string value into our string:

txt = 'This is {}' print(txt.format('text'))

Result of code execution:

'This is text'

Example

You can also specify a number:

txt = 'This is {}' print(txt.format(123))

Result of code execution:

'This is 123'

Example

Now let's substitute a list element into the string:

txt = 'This is {}' lst = ['a', 'b', 'c'] print(txt.format(lst[0]))

Result of code execution:

'This is a'

See also

  • method replace,
    which searches and replaces a substring in a string
  • method zfill,
    which fills the beginning of the string with zeros
  • method rjust,
    which aligns and fills the text in a line to the right
  • method ljust,
    which aligns and fills the text in a line to the left
English
AfrikaansAzərbaycanБългарскиবাংলাБеларускаяČeštinaDanskDeutschΕλληνικάEspañolEestiSuomiFrançaisहिन्दीMagyarՀայերենIndonesiaItaliano日本語ქართულიҚазақ한국어КыргызчаLietuviųLatviešuМакедонскиMelayuမြန်မာNederlandsNorskPolskiPortuguêsRomânăРусскийසිංහලSlovenčinaSlovenščinaShqipСрпскиSrpskiSvenskaKiswahiliТоҷикӣไทยTürkmenTürkçeЎзбекOʻzbekTiếng Việt
We use cookies for website operation, analytics, and personalization. Data processing is carried out in accordance with the Privacy Policy.
accept all customize decline