Using the Python string.format() Method
The {} will be replaced by the positional arguments
'His gpa is {0:^10.4f} and his ACT is {1:d}'.format(4.65734, 33)
The arguments after the : tell Python how the data should appear
Order of arguments --> [Index]:[Flags][Width][Precision][Type]
Flags: Special formatting characteristics - ^ is center alignment
Index: Specifies which element from the .format() argument
Precision: How many decimal places to show
{0:^10.4f}
Colon: Divides the data index from the data formatting instructions
Type: What type to display? String (s), integer (d), float (f), etc.
Width: Controls how many spaces to display this particular item.
Format Codes to Remember
Title here
Important Flags to Remember
Lorem ipsum dolor
Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod.
- Lorem ipsum dolor sit amet.
- Consectetur adipiscing elit.
- Sed do eiusmod tempor incididunt ut.
- Labore et dolore magna aliqua.
.format() method
Andrew Montanus
Created on July 10, 2023
Understanding Python's .format() method
Start designing with a free template
Discover more than 1500 professional designs like these:
View
Akihabara Connectors Infographic
View
Essential Infographic
View
Practical Infographic
View
Akihabara Infographic
View
Interactive QR Code Generator
View
Witchcraft vertical Infographic
View
Halloween Horizontal Infographic
Explore all templates
Transcript
Using the Python string.format() Method
The {} will be replaced by the positional arguments
'His gpa is {0:^10.4f} and his ACT is {1:d}'.format(4.65734, 33)
The arguments after the : tell Python how the data should appear
Order of arguments --> [Index]:[Flags][Width][Precision][Type]
Flags: Special formatting characteristics - ^ is center alignment
Index: Specifies which element from the .format() argument
Precision: How many decimal places to show
{0:^10.4f}
Colon: Divides the data index from the data formatting instructions
Type: What type to display? String (s), integer (d), float (f), etc.
Width: Controls how many spaces to display this particular item.
Format Codes to Remember
Title here
Important Flags to Remember
Lorem ipsum dolor
Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod.