Skip to Content

Advertisement

You (Level 0)
0% Complete
Last Badge Earned
None

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

For a more tangible and better look into the Python language, consider reading the following book. It's an excellent read.

Your code will execute in this window.



Let's explore some more tutorials or topics!

Next Tutorial Previous Tutorial

Advertisement

If you enjoyed this resource, support me by sharing this page with others.

Comment on

Show Comments
  • User

    HDNua Dec. 14, 2016, 11:53 p.m.

    print("생일 축하 메시지: %s, %s, %s에게." %"도영" %"한도" %"영도")

  • User

    AlanTuring Oct. 9, 2015, 1:08 a.m.

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

  • User

    BRIAN March 26, 2015, 7:20 a.m.

    lam loving it!! Bill gates look out for me!, coz lam lam coming with my great programming skills !!! :)

  • User

    BRIAN March 26, 2015, 7:20 a.m.

    lam loving it!! Bill gates look out for me!, coz lam lam coming with my great programming skills !!! :)

  • User

    Atlas Feb. 26, 2015, 8:34 p.m.

    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.

  • User

    Atlas Feb. 26, 2015, 8:34 p.m.

    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.

  • User

    yang jun June 19, 2014, 7:10 p.m.

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

  • User

    Keefe2014 May 6, 2014, 9:49 p.m.

    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?

  • User

    whdqh1550 April 16, 2014, 8:52 p.m.

    what is the alphabet right next the number that on 20?!

  • User

    Anton Boers April 7, 2014, 9:35 a.m.

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

  • User

    Geeoon March 20, 2014, 10:57 a.m.

    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?

  • User

    wisevariable March 6, 2014, 12:14 p.m.

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

  • User

    Lotfi GHAZOUANI March 3, 2014, 8:34 a.m.

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

  • User

    lbnewbie Dec. 16, 2013, 5:45 p.m.

    good but challenging for a newb - some set tasks could help

  • User

    Nitish P Nov. 30, 2013, 8:45 a.m.

    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.

  • User

    Tai Toki Oct. 14, 2013, 1:07 a.m.

    I get confused when to indent print and when not to.

  • User

    Abbas92 Oct. 6, 2013, 6:25 p.m.

    a ="abcdefghijklmnopqrstuvwxyz" print('sum %.2s' % a)

  • User

    Brakmaren Sept. 20, 2013, 3:56 a.m.

    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.

  • User

    Rebarakaz Sept. 14, 2013, 5:02 a.m.

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

  • User

    Rebarakaz Sept. 14, 2013, 4:59 a.m.

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



Advertisement