Python - Format Strings
Python - Format Strings
HTML
CSS JAVASCRIPT SQL PYTHON JAVA PHP HOW TO W3.CSS C
String Format
As we learned in the Python Variables chapter, we cannot combine strings and
numbers like this:
age = 36
txt = "My name is John, I am " + age
print(txt)
Try it Yourself »
But we can combine strings and numbers by using the format() method!
The format() method takes the passed arguments, formats them, and places them
in the string where the placeholders {} are:
Example
Use the format() method to insert numbers into strings:
age = 36
txt = "My name is John, and I am {}"
https://www.w3schools.com/python/python_strings_format.asp 1/6
10/5/23, 6:47 PM Python - Format Strings
print(txt.format(age))
Tutorials Exercises Services My W3Schools
Try itCSS
HTML YourselfJAVASCRIPT
» SQL PYTHON JAVA PHP HOW TO W3.CSS C
The format() method takes unlimited number of arguments, and are placed into the
respective placeholders:
Example
quantity = 3
itemno = 567
price = 49.95
myorder = "I want {} pieces of item {} for {} dollars."
print(myorder.format(quantity, itemno, price))
Try it Yourself »
You can use index numbers {0} to be sure the arguments are placed in the correct
placeholders:
Example
quantity = 3
itemno = 567
price = 49.95
myorder = "I want to pay {2} dollars for {0} pieces of item {1}."
print(myorder.format(quantity, itemno, price))
Try it Yourself »
❮ Previous Next ❯
https://www.w3schools.com/python/python_strings_format.asp 2/6
10/5/23, 6:47 PM Python - Format Strings
HTML
CSS JAVASCRIPT SQL PYTHON JAVA PHP HOW TO W3.CSS C
ADVERTISEMENT
COLOR PICKER
https://www.w3schools.com/python/python_strings_format.asp 3/6
10/5/23, 6:47 PM Python - Format Strings
HTML
CSS JAVASCRIPT SQL PYTHON JAVA PHP HOW TO W3.CSS C
ADVERTISEMENT
ADVERTISEMENT
https://www.w3schools.com/python/python_strings_format.asp 4/6
10/5/23, 6:47 PM Python - Format Strings
HTML
CSS JAVASCRIPT SQL PYTHON JAVA PHP HOW TO W3.CSS C
Top Tutorials
HTML Tutorial
CSS Tutorial
JavaScript Tutorial
How To Tutorial
SQL Tutorial
Python Tutorial
W3.CSS Tutorial
Bootstrap Tutorial
PHP Tutorial
Java Tutorial
C++ Tutorial
jQuery Tutorial
Top References
HTML Reference
CSS Reference
JavaScript Reference
SQL Reference
Python Reference
W3.CSS Reference
Bootstrap Reference
PHP Reference
HTML Colors
Java Reference
Angular Reference
jQuery Reference
CSS Examples
Tutorials Exercises
JavaScript Examples
Services CSS Certificate
JavaScript Certificate
My W3Schools
How To Examples Front End Certificate
HTML
CSS SQL Examples
JAVASCRIPT SQL PYTHON SQL Certificate
JAVA PHP HOW TO W3.CSS C
Python Examples Python Certificate
W3.CSS Examples PHP Certificate
Bootstrap Examples jQuery Certificate
PHP Examples Java Certificate
Java Examples C++ Certificate
XML Examples C# Certificate
jQuery Examples XML Certificate
FORUM ABOUT
W3Schools is optimized for learning and training. Examples might be simplified to
improve reading and learning.
Tutorials, references, and examples are constantly reviewed to avoid errors, but we
cannot warrant full correctness
of all content. While using W3Schools, you agree to have read and accepted our
terms of use, cookie and privacy policy.
https://www.w3schools.com/python/python_strings_format.asp 6/6