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

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

Result

abcdefghijklmnopqrst

If you enjoyed this resource, support me by sharing this page with others.
Share on Facebook
Facebook
Share on Google+
Google+
Share on StumbleUpon
StumbleUpon
Digg this
Digg
Tweet about this on Twitter
Twitter
Share on Reddit
Reddit

Leave a Comment

  1. @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
    >>>

  2. @Vernon: Oh, I see..I’ve just tested that code using the simulator and you’re right..The result is 123 🙂
    But in my IDLE, I got the result 123.440000
    Thanks mate..

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

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

  5. There is a difference between the code simulator and Python console for the first line in the first example.

  6. I have to agree ibnewbie some tasks beside the quiz would make understanding stuff like this easier for some people.

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

  8. It’s okay, but could be nice with some more examples, like saying what situations this could come in handy n’ such.

  9. 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?

  10. the software has a bug, when input a=12.345678,print(‘%f’ %a) ,get 12.but on my python environment , get 12.345678

  11. It would be nice if there was a plain word glossary at the end of all this to look up various technical words used in these tutorials.

  12. It would be nice if there was a plain word glossary at the end of all this to look up various technical words used in these tutorials.

  13. Ok, I do not understand this example of formatting a string. Have to look it up somewhere since he does not explain it.

Leave a Reply

Your email address will not be published. Required fields are marked *

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