Can be used for the following
Numbered Placeholders
Add Width, padding
Access elements of sequences and dictionaries
Access object Attributes
Traditional (i.e., old) way to format strings in Python was with the % operator and a formation string containing fields designated with percent signs.
The new, improved method of string formatting uses the format() method.
It takes a format string and one or more arguments.
The format string containers placeholders which consist of {} with may contain formatting details
By Default th placeholders are numbered from the left to right, starting at 0.
This corresponds to the order of arguments to format().
Formatting can be added, preceded by a colon.
Placeholders can be manually numbered. This is handy when you want to use a format() parameter more than once.
>>> "Try one of these: {0}.png. {0}.bmp {0}.jpg {0}.pdf".format('penguin')
Try one of these: penguin.png penguin.bmp penguin.jpg penguin.pdf
You can create a string with the "f" at the beginning to allow you to insert variables as part of the string
name = "Aaron"
greeting = f"Hello {name}!!!"
print(greeting)
>> Hello Aaron!!!
This is the same as using the f in the print statement:
name = "Aaron"
print(f"Hello {name}!!!")
>> Hello Aaron!!!
name = "Aaron"
greeting = "Hello, {}!"
with_name = greeting.format(name)
print(with_name)
>> Hello, Aaron!
greeting_phrase = "{}, {}. How are you today?"
name = "Aaron"
greeting_with_name = greeting_phrase.format("Greetings", name)
print(greeting_with_name)
>> Hello, Aaron. How are you today?