Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Python

Jan Lundeen
Jan Lundeen
5,881 Points

In python, what is the reason for using .format --- why is it used and in what situations is it needed

Hi,

This sounds like a silly question, but why do we use .format in Python? Python is the first computer language I've seen, so it would be helpful to get some perspective on this. Why is the .format command used? In what situations is important to use it? I looked in my notes and googled my questions, but I can't find a good answer that makes sense (in layman's terms for us newbies).

Thanks,

Jan Lundeen

1 Answer

Hi there,

There are tons of reasons to use '.format', but I think the most common reason is that more often than not, you'll need to input a value that changes, or that you may not know at the time of the program running.

For example, if I set my age to a variable, and I've written out code to update my age variable each year, then I would want to use .format on the output string, instead of adjusting the string with my new age each time, right?

It will become much more clear as you use it more often, so no worries.

Hope that helped!

Jan Lundeen
Jan Lundeen
5,881 Points

So having a variable that changes is one reason, can you give me examples of other times where it would be helpful? I see it used in many places, but I can't find a common thread for when it is used and when it is not used. It made me think that I don't completely understand the .format command. Can you tell me what exactly happens when we format something in python? Thanks!

I can definitely try, but I'm still learning python myself, so forgive me if I make it more confusing!

Essentially when you run .format(), you're calling the argument you enclose in the parentheses to be entered. So if my program is the below :

name = 'Sean'
subject = 'Treehouse loves {}'.format(name)
print(subject)

Then that means I'm telling Python to use the name variable in those placeholders. When you're writing the code yourself, it doesn't exactly seem like that much of a shortcut. But if we created the program with an input option, where you can put in your own name, then you'd want to set it to a variable. If I didn't know your name and you were using it, then essentially, the only way to make sure it can still put in the right name is to .format() the string, with your information. I'm not sure if you reached that point yet on the courses, but I'll put an example below.

name = input('Please enter your name: ')
subject = 'Treehouse loves {}.'.format(name)
print(subject)

It's mostly the same, but the big difference is that you can enter your name, I can enter my name, Kenny can enter his name, etc. It's more adaptable to a wider group, as it isn't preset and defined to one attribute.

In other words, you should consider formatting the output method if the information can be different or updated. I'm not sure if I made much sense, but I would definitely suggest watching the course videos, as over time it kind of becomes more second nature - Kenneth is a MUCH better teacher than me haha.

Jan Lundeen
Jan Lundeen
5,881 Points

Hi Sean,

I appreciate you taking a shot at it. Since you are just learning python, it good to try to explain it to someone else. If you can do that, you know that you've mastered it.

I understand the part about passing variables into the parentheses. However, it seems like .format is a generic command, since it's used with other commands such as .join(), .split() and print(). For example, .join() will join together statements and split will separate or parse a statement. I notice that sometimes .format() is used with some of these functions and sometimes, .format() is not used. I think .format affects the page layout, but I'm really not sure how. As I recall, you said that you should consider using .format if the info will be different or updated. Maybe that's why

I haven't watched the video on input yet, but I can understand how it would be valuable to have a command that allows a user to type in input.

I've watched Kenneth's videos, but he hasn't discussed .format yet. It's been used in some of the code challenges though. Can I ask Kenneth directly to explain .format?

Thanks,

Jan