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

Python - Format Strings

The document discusses formatting strings in Python using the format() method. It provides examples of inserting numbers and variables into strings using format placeholders. It also covers using index numbers to specify placement if the arguments are not in the correct order.

Uploaded by

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

Python - Format Strings

The document discusses formatting strings in Python using the format() method. It provides examples of inserting numbers and variables into strings using format placeholders. It also covers using index numbers to specify placement if the arguments are not in the correct order.

Uploaded by

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

10/5/23, 6:47 PM Python - Format Strings

 Tutorials  Exercises  Services   My W3Schools

HTML
 CSS JAVASCRIPT SQL PYTHON JAVA PHP HOW TO W3.CSS C

Python - Format - Strings


❮ Previous Next ❯

String Format
As we learned in the Python Variables chapter, we cannot combine strings and
numbers like this:

Example Get your own Python Server

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 »

Learn more about String Formatting in our String Formatting chapter.

❮ Previous Next ❯

https://www.w3schools.com/python/python_strings_format.asp 2/6
10/5/23, 6:47 PM Python - Format Strings

 Tutorials  Exercises  Services   My W3Schools

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

 Tutorials  Exercises  Services   My W3Schools

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

 Tutorials  Exercises  Services   My W3Schools

HTML
 CSS JAVASCRIPT SQL PYTHON JAVA PHP HOW TO W3.CSS C

 SPACES UPGRADE NEWSLETTER

GET CERTIFIED REPORT ERROR

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

Top Examples Get Certified


https://www.w3schools.com/python/python_strings_format.asp 5/6
10/5/23, 6:47 PM Python - Format Strings
HTML Examples HTML Certificate

 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.

Copyright 1999-2023 by Refsnes Data. All Rights Reserved. W3Schools is Powered by


W3.CSS.

https://www.w3schools.com/python/python_strings_format.asp 6/6

You might also like