Home » Tutorials » Python » Formatting

Formatting

We waited a little bit to talk about formatting because it might get a little intense with how much you can do and how easily you can do things with variables in Python. Formatting in Python is a little bit odd at first. But, once you get accustomed to it, you’ll be glad you did.

Formatting Numbers as Strings

Example
print('The order total comes to %f' % 123.44)
print('The order total comes to %.2f' % 123.444)
Result The order total comes to 123.440000
The order total comes to 123.44

Ya, I told you it’s a little weird. The f following the first % is short for float here because we have floating numbers and Python has a specific way of dealing with formatting decimals. The left % tells Python where you want to put the formatted string. The value following the right % is the value that we want to format. So, Python reads through the string until it gets to the first % then Python stops and jumps to the next %. Python takes the value following the second % and formats it according to the first %. Finally, Python places that second value where the first % is. We can use a single value such as a string or a number. We can also use a tuple of values or a dictionary. Alright, this is great, but what about formatting strings?

Formatting Strings

Strings are just like how we were formatting the numbers above except we will use a s for string instead of an f like before. Usually, you will only want to format a string to limit the number of characters. Let’s see it in action:

Example
a ="abcdefghijklmnopqrstuvwxyz"
print('%.20s' % a)
Result abcdefghijklmnopqrst


Link/cite this page

If you use any of the content on this page in your own work, please use the code below to cite this page as the source of the content.

  • Stewart, Suzy. "Formatting". After Hours Programming. Accessed on March 16, 2024. https://www.afterhoursprogramming.com/tutorial/python/formatting/.

  • Stewart, Suzy. "Formatting". After Hours Programming, https://www.afterhoursprogramming.com/tutorial/python/formatting/. Accessed 16 March, 2024.

  • Stewart, Suzy. Formatting. After Hours Programming. Retrieved from https://www.afterhoursprogramming.com/tutorial/python/formatting/.



20 thoughts on “Formatting”

  1. print(‘The order total comes to %f’ % 123.44)

    this situation we have just one parameter for formatting,

    how can I define the format for two or more parameters when I use only one "print" in python?

  2. I’m not sure if the code simulator is wrong or the output for the example is wrong, but, when i used the code simulator and made my own program using the first example print(‘%f’ % 123.44)
    it came out with 123 instead of 123.440000 shouldn’t it be 123?

  3. Similar to C style formatting very cool.But i have observed something.
    >>> print("The order total comes to %f" % 123.44)
    The order total comes to 123.440000 –> What is the maximum values displayed? is this is 6 the default limit.
    >>> print("The order total comes to ", 123.44)
    The order total comes to 123.44 –> Normal display of values
    >>> print("The order total comes to %.1f", 12.4569)
    The order total comes to %.1f 12.4569 –> Formatting is dropped
    >>> print("The order total comes to %.1f" % 12.4669)
    The order total comes to 12.5 –> But the value is rounded off instead of displaying just "12.4" is this the default behaviour of python formatting.

  4. Could be wrong (newbie) but i think you have violated youre own (SPACE) thing.
    Example reads
    a ="abcdefghijklmnopqrstuvwxyz"
    Will not compile unless space is added.
    a =[HERE]"abcdefghijklmnopqrstuvwxyz"
    Else thanks for an easy to follow tutorial.

  5. @Vernon: Are you sure about that? I got the exact result as in the example above.
    Here is my result:
    >>> print(‘The order total comes to %f’ % 123.44)
    The order total comes to 123.440000
    >>>

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.