Heads up! To view this whole video, sign in with your Treehouse account or enroll in your free 7-day trial. Sign In Enroll
Well done!
You have completed Hello Python!
You have completed Hello Python!
Preview
Take your strings and inputs to the next level with string formatting.
This video doesn't have any notes.
Related Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign upRelated Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign up
Let's take a look at the code
we've written so far.
0:00
Looking great, but
there's one thing that's bothering me.
0:03
We're printing out these pieces of a
sentence using multiple print statements,
0:06
making them look a bit messy in
the console instead of a nice single
0:11
line sentence.
0:15
Was it bothering you too?
0:18
Well, you're thinking,
there's got to be a better way, Megan.
0:19
[LAUGH] Well, I'm here to tell you there
is, it's called string formatting.
0:23
It allows us to pass in our
variables into our strings.
0:29
It will help us reduce the amount of
code we've written and be a bit neater.
0:34
Now, there are a few ways to do this,
I'm only going to show you one way, and
0:38
if you decide to move forward with Python,
you will learn the other methods.
0:43
In front of the My name is string,
in between the opening parenthesis and
0:49
the first quotation mark,
add the letter f.
0:56
This stands for format.
1:00
It tells Python that there will be some
extra tasks to do with this string,
1:03
before it prints it to the console.
1:07
Then, at the end of the message,
1:11
we can add our name variable, and
1:14
it will be inside curly brackets, {name}.
1:18
This tells Python to find the variable
name and add what it equals to
1:24
the string right here, then you can
delete the print statement below.
1:29
And let's see this in action.
1:36
Save and let's run the file.
1:39
Enter in any name you want.
1:45
And voila, our name was placed
inside of your message.
1:51
Now, can you do the same thing for
the last name below?
1:57
Go ahead and pause me and
try converting it on your own.
2:02
When you finished or if you get stuck,
unpause me and see how I did it.
2:05
I'm going to add a space.
2:13
And then, curly bracket, last_name,
2:15
closing curly bracket,
and remove last_name.
2:19
Save and run the file to check your work.
2:25
Sweet, it all works.
2:34
It's time to practice.
2:37
Create a new variable called color.
2:39
And ask the user what
their favorite color is.
2:45
Then print out a message in the console,
my favorite color is, and
2:48
add the variable into the string
using string formatting.
2:52
Pause me and give it a go.
2:57
Okay, let's tackle this challenge.
3:02
Color = input ( 'What is
3:04
your favorite color?'),
3:09
and then down here,
3:15
below our print statement,
3:19
we'll do print ( f 'My favorite
3:24
color is {color}').
3:31
Save and run the file.
3:36
Megan Amendola.
3:45
My name is Megan,
my favorite color is green.
3:53
Challenge complete.
3:56
What other questions
can you ask your user?
3:58
Practice creating variables,
asking for input, and
4:01
printing formatted strings on your own.
4:04
The more you practice,
the more the knowledge will stick.
4:08
You need to sign up for Treehouse in order to download course files.
Sign upYou need to sign up for Treehouse in order to set up Workspace
Sign up