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
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:
print(“?? ?? ???: %s, %s, %s??.” %”??” %”??” %”??”)
Ok, I do not understand this example of formatting a string. Have to look it up somewhere since he does not explain it.
lam loving it!! Bill gates look out for me!, coz lam lam coming with my great programming skills !!! 🙂
lam loving it!! Bill gates look out for me!, coz lam lam coming with my great programming skills !!! 🙂
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.
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.
the software has a bug, when input a=12.345678,print(‘%f’ %a) ,get 12.but on my python environment , get 12.345678
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?
what is the alphabet right next the number that on 20?!
It’s okay, but could be nice with some more examples, like saying what situations this could come in handy n’ such.
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?
I have to agree ibnewbie some tasks beside the quiz would make understanding stuff like this easier for some people.
There is a difference between the code simulator and Python console for the first line in the first example.
good but challenging for a newb – some set tasks could help
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.
I get confused when to indent print and when not to.
a ="abcdefghijklmnopqrstuvwxyz"
print(‘sum %.2s’ % a)
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.
@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..
@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
>>>