How does .format() work? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

How does .format() work?

I don't really get it

11th Dec 2018, 7:50 PM
Ric2908
Ric2908 - avatar
6 Answers
+ 6
For example you can 'copy paste' variables into your output. age = 18 print('I am {} years old!'.format(age)) -> I am 18 years old!
11th Dec 2018, 8:16 PM
HonFu
HonFu - avatar
+ 7
Just so you know, since Python 3.6 f-strings were introduced, making the whole formatting issue a looooot easier, cleaner and more intuitive :) But if you still need a lesson the .format() way, here it is: https://www.sololearn.com/learn/Python/2455/?ref=app
11th Dec 2018, 9:01 PM
Kuba Siekierzyński
Kuba Siekierzyński - avatar
+ 3
Like..... myInt = 5 f"some string{myInt}” or "some string %d” myInt
11th Dec 2018, 11:39 PM
Dan Rhamba
Dan Rhamba - avatar
+ 2
...and see here https://pyformat.info/
11th Dec 2018, 8:31 PM
KrOW
KrOW - avatar
+ 2
Kuba Siekierzyński Yes, but its always useful to know for who dont want switch to py3 😁
11th Dec 2018, 9:04 PM
KrOW
KrOW - avatar