Previously, I introduced you how to dynamically insert
the value of a variable inside a string.
So here we constructed Hello and whatever value
the user entered in the input function.
So this worked with Python 2 and 3.
This works with Python 3.6 and later versions.
Now, what if we want to insert
more than one value inside a string?
How to do that?
For example, let's say the first input function
will ask us for the name, and the
second will ask us for the surname.
Let's call this variable name and this one surname.
To use this version of string formatting with
two variables, you'd need to do this.
Instead of just one variable, you need to use round
brackets and then enter all the variables you want to
insert in the string name and surname in this case.
Now, if you execute this, you're going to get an error.
So you need to do something more there.
Let's say Ardit Sulce, and you got a TypeError.
Not all arguments converted during string formatting, which
means that the arguments are name and surname.
Not all of them were converted because
only name was converted because you only
had one placeholder here, percentage s.
You want to have two placeholders, one for surname too.
If you execute that well, we get another error.
But that's not related to this line here.
This line is perfectly fine.
It works.
Why does it work?
Well, because you can see that the error happened.
So this is where the trace back starts.
You see that the error happens in file basics.py, which
is the file where we have the code in line 5,
which means that above line 5, everything is okay.
So this line now has passed the error checking.
It's free of errors.
So this is the line that has
the error, and it's a NameError.
name user_input is not defined.
So this user_input here has
not been defined in our code.
We don't have a variable with that name.
We're just using it here.
But we didn't define it.
And here is a trick.
If you don't want to execute that
line, you can comment it out.
So by adding this symbol here,
Python will ignore this line.
It will treat it as a comment,
[Author typing]
not as code.
So save the script.
Execute, enter your input as a user.
And this is the output we get this time.
So that was a good exercise on how to fix errors.
Now back to our string formatting operations.
Let's see how this now will work with two variables.
So, user_input is not a variable anymore.
We want to use name.
So Hello name,
and then we want to have a space here.
So this is a white space.
So name, white space, surname.
And it's as easy as that. Execute,
[Author typing]
and you get the output.
Hello, Ardit, Sulce.
So that's the message variable, the
string of the message variable.
So in this way, you can put
as many variables as you like.
For example, what's up?
When could be a variable today.
The string today is part of the when variable.
The when variable will be replaced in here by the
value that it has, it currently has, which is today.
Execute that.
[Author typing]
And that's how it works.
And that's what you need to
know about string formatting in Python.
[Author typing]