Python- tutoriaal

Python TUIS Python Intro Python Begin Python-sintaksis Python-kommentaar Python veranderlikes Python-datatipes Python-nommers Python Casting Python Strings Python Booleans Python-operateurs Python-lyste Python Tuples Python-stelle Python Woordeboeke Python As...Anders Python While Loops Python vir lusse Python-funksies Python Lambda Python-skikkings Python-klasse/-voorwerpe Python Erfenis Python Iterators Python-omvang Python-modules Python-datums Python Wiskunde Python JSON Python RegEx Python PIP Python Probeer ... Behalwe Python-gebruikersinvoer Python String Formatering

Lêerhantering

Python-lêerhantering Python Lees lêers Python Skryf/skep lêers Python verwyder lêers

Python-modules

NumPy Tutoriaal Panda Walkthrough Scipy Tutoriaal

Python Matplotlib

Matplotlib Intro Matplotlib Begin Matplotlib Pyplot Matplotlib Plotte Matplotlib Merkers Matplotlib-lyn Matplotlib-etikette Matplotlib-rooster Matplotlib Subplotte Matplotlib Scatter Matplotlib Bars Matplotlib Histogramme Matplotlib sirkeldiagramme

Masjienleer

Aan die gang kom Gemiddelde mediaanmodus Standaard afwyking Persentiel Dataverspreiding Normale dataverspreiding Strooi plot Lineêre regressie Polinoomregressie Meervoudige regressie Skaal Trein/toets Besluitboom

Python MySQL

MySQL Begin MySQL Skep databasis MySQL Skep tabel MySQL-insetsel MySQL Kies MySQL Waar MySQL Bestel deur MySQL verwyder MySQL Drop Table MySQL-opdatering MySQL-limiet MySQL Sluit aan

Python MongoDB

MongoDB Begin MongoDB Skep databasis MongoDB Skep versameling MongoDB-insetsel MongoDB Vind MongoDB-navraag MongoDB Sorteer MongoDB verwyder MongoDB Drop Collection MongoDB-opdatering MongoDB-limiet

Python-verwysing

Python Oorsig Python ingeboude funksies Python-stringmetodes Python Lys Metodes Python Woordeboek Metodes Python Tuple Metodes Python Stel metodes Python-lêermetodes Python sleutelwoorde Python-uitsonderings Python Woordelys

Moduleverwysing

Ewekansige module Versoeke Module Statistiek Module Wiskunde Module cMath-module

Python Hoe om

Verwyder lys duplikate Draai 'n snaar om Voeg twee getalle by

Python voorbeelde

Python voorbeelde Python-samesteller Python-oefeninge Python Vasvra Python-sertifikaat

Python String format() Metode

❮ Stringmetodes


Voorbeeld

Voeg die prys in die plekhouer in, die prys moet in vaste punt, twee-desimale formaat wees:

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

Definisie en gebruik

Die format()metode formateer die gespesifiseerde waarde(s) en voeg dit in die string se plekhouer in.

Die plekhouer word gedefinieer met behulp van krullerige hakies: {}. Lees meer oor die plekhouers in die Plekhouer-afdeling hieronder.

Die format()metode gee die geformateerde string terug.


Sintaksis

string.format(value1, value2...)

Parameterwaardes

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.

Die plekhouers

Die plekhouers kan geïdentifiseer word deur benoemde indekse {price}, genommerde indekse {0}of selfs leë plekhouers te gebruik {}.

Voorbeeld

Gebruik verskillende plekhouerwaardes:

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)

Formateer tipes

Binne die plekhouers kan jy 'n formateringtipe byvoeg om die resultaat te formateer:

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

❮ Stringmetodes