0% found this document useful (0 votes)
46 views

Python String Formatting

The document discusses different methods for formatting strings in Python using the format() method. It provides examples of using format() to insert values into placeholders in a string, including using indexes to specify positions and named indexes to reference values. Parameters can be formatted when inserting into the string using placeholders, such as formatting a number to two decimal places. Multiple values can also be inserted into a string using format() by passing a tuple of values.

Uploaded by

kevin1234thenula
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
46 views

Python String Formatting

The document discusses different methods for formatting strings in Python using the format() method. It provides examples of using format() to insert values into placeholders in a string, including using indexes to specify positions and named indexes to reference values. Parameters can be formatted when inserting into the string using placeholders, such as formatting a number to two decimal places. Multiple values can also be inserted into a string using format() by passing a tuple of values.

Uploaded by

kevin1234thenula
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

7/15/2021 Python String Formatting

To make sure a string will display as expected, we can format the result with
the format() method.

String format()
The format() method allows you to format selected parts of a string.

Sometimes there are parts of a text that you do not control, maybe they come from a database,
or user input?

To control such values, add placeholders (curly brackets {} ) in the text, and run the values
through the format() method:

Example
Add a placeholder where you want to display the price:

price = 49
txt = "The price is {} dollars"
print(txt.format(price))

Try it Yourself »

You can add parameters inside the curly brackets to specify how to convert the value:

Example
Format the price to be displayed as a number with two decimals:

txt = "The price is {:.2f} dollars"

Try it Yourself »

Check out all formatting types in our String format() Reference.

Multiple Values
If you want to use more values, just add more values to the format() method:
https://www.w3schools.com/python/python_string_formatting.asp 1/3
7/15/2021 Python String Formatting

print(txt.format(price, itemno, count))

And add more placeholders:

Example
quantity = 3
itemno = 567
price = 49
myorder = "I want {} pieces of item number {} for {:.2f} dollars."
print(myorder.format(quantity, itemno, price))

Try it Yourself »

Index Numbers
You can use index numbers (a number inside the curly brackets {0} ) to be sure the values are
placed in the correct placeholders:

Example
quantity = 3
itemno = 567
price = 49
myorder = "I want {0} pieces of item number {1} for {2:.2f} dollars."
print(myorder.format(quantity, itemno, price))

Try it Yourself »

Also, if you want to refer to the same value more than once, use the index number:

Example
age = 36
name = "John"

https://www.w3schools.com/python/python_string_formatting.asp 2/3
7/15/2021 Python String Formatting

txt = "His name is {1}. {1} is {0} years old."


print(txt.format(age, name))

Try it Yourself »

Named Indexes
You can also use named indexes by entering a name inside the curly brackets {carname} , but
then you must use names when you pass the parameter values txt.format(carname =
"Ford") :

Example
myorder = "I have a {carname}, it is a {model}."
print(myorder.format(carname = "Ford", model = "Mustang"))

https://www.w3schools.com/python/python_string_formatting.asp 3/3

You might also like