Welcome to PyBeginners β your go-to place to learn Python programming the easy way! π
Whether you're starting from scratch or revisiting the basics, this channel is built just for YOU. We break down Python concepts into bite-sized, beginner-friendly videos using real-life examples, everyday language, and fun visuals.
Think of PyBeginners as your coding buddy β here to help you:
β
Understand the why behind every concept
β
Build confidence, one small step at a time
β
Learn coding without feeling overwhelmed
π Check out more learning resources:
π Blogs: https://www.pybeginners.com/
πΌ LinkedIn: https://www.linkedin.com/newsletters/pybeginners-7267881715929415681/
π New videos every week β covering Python basics, hands-on exercises, projects, and career tips!
π Subscribe, join the PyBeginners family, and let's make Python simple together π»π¬
#LearnPython #PyBeginners #PythonForBeginners #PythonMadeEasy
Show More Show Less View Video Transcript
0:02
Hey everyone, welcome back to the Python
0:04
basics series. I'm so excited for
0:07
today's episode because we are diving
0:08
into something that will make your code
0:10
look awesome and readable, which is str
0:14
formatting in Python. So have you ever
0:18
wondered how to create sentences like
0:20
hello Mary you are 28 years old where
0:24
you can just drop in variables wherever
0:27
you want.
0:30
So well we'll learn exactly how to do
0:32
that and also we'll work with dot format
0:36
and f strings. So let's jump in.
0:41
So first let's understand why do we even
0:44
need to format strings.
0:47
Well let's say you have a variable for
0:50
someone's name and age and you want to
0:53
print it like for example
0:56
name equal to Mary
1:01
age equal to 28
1:04
and you want to print hello Mary you are
1:07
28 years old. Sure you could do chunky
1:11
concatenation like this we have done
1:13
before. So which is print
1:17
hello
1:20
or you can do something like hello
1:29
name.
1:34
We need dot here and u R
1:50
we are you know converting type
1:52
conversion.
1:54
So we are converting the number to
1:56
string here. And we need this in
2:05
you can do something like this, right?
2:11
But honestly that's really messy, hard
2:14
to read and easy to mess up. You can
2:16
really mess up with that when you are
2:18
really on stress or on pressure.
2:21
Instead, Python gives us better ways
2:24
with dot format and f strings.
2:30
So, let's start with classic dot format
2:33
method. Like for example, you gave your
2:35
name Mary and age 28.
2:38
And let's create one more variable which
2:41
is message.
2:46
And here you're going to do hello.
2:50
And let's give a strange brackets here.
2:52
Don't worry about it. We are going to
2:54
talk about it. And you can just give you
2:56
r. And again the strange brackets which
3:00
are flower brackets
3:02
and
3:04
old
3:08
dot
3:10
and here we are going to use format
3:13
and you are going to give name and age.
3:19
So here hello Mary you want Mary. So the
3:23
name will be substituted here and you
3:27
are dash years old and the age will be
3:30
substituted here. You don't have to
3:31
worry about the uh you know the priority
3:34
of giving the things the first thing
3:36
like it's like first come first serve.
3:39
So if you give name as first then it
3:42
will store name here. If you give age
3:44
then it will store age here. So it will
3:46
follow the format that you gave. So the
3:50
dot format will substitute these
3:52
brackets with the values that you gave.
3:56
So now
3:58
when you print the message
4:04
and now when you run this
4:08
you'll see hello Mary you are 28 years
4:11
old which is if you see the difference
4:14
this is really complex and it made
4:16
things easy for us to understand.
4:21
So what's happening here? So inside the
4:24
string we write these flower brackets
4:27
where we want our variables to go. Then
4:31
after the string we call dot format and
4:35
give it the variables in order. You have
4:38
to follow the order. So Python swaps
4:41
each flower bracket with the
4:44
corresponding variable.
4:46
So the output will be hello Mary you are
4:49
28 years old. The pro tip is you can
4:53
reuse variables multiple times by
4:55
referencing their position. But
4:58
honestly, if you're on Python new
5:01
version, there is also a better way. So,
5:04
this is how you can use formatting. And
5:06
another awesome thing about uh dot
5:09
format is formatting numbers as well.
5:11
So, especially if you want to control
5:14
decimal places like I will show you that
5:17
as well.
5:23
Like you give price equal to some number
5:27
24.99
5:30
and discount
5:36
0.15
5:38
and message.
5:48
So the discounted
5:53
price is
5:57
again flower brackets
6:00
and you want it in decimal. So for this
6:03
you have to do
6:05
colon
6:07
dot up to two points. So 2 f
6:12
and then end of the string
6:16
we need dot here and then end of the
6:18
string we need format. So dot always
6:22
keep that in mind whenever you're using
6:24
the format method start with dot format.
6:30
Format
6:33
we need price
6:37
and
6:40
So we are actually calculating the
6:42
discounted price. So we have to write
6:45
the formula for discounted price into
6:50
So the formula will be 1 minus
6:54
discount.
6:58
So this will be the discount. So 1 minus
7:01
discount into price will be going here.
7:05
And now you can just check print
7:08
message.
7:12
Don't worry about the logic like how we
7:15
are calculating the discount. If you
7:17
still feel it's a bit complex, you don't
7:20
you don't have to worry about this. But
7:22
the point is how you will show the
7:25
decimal points in using the format
7:29
method. So that's the point. So the this
7:32
so the discounted price is 21.24. So the
7:36
price is $25 and the discount we have is
7:39
15%. So it will be 21.24.
7:44
So the 2F means
7:47
this means formatting the number as a
7:51
float with two decimal places.
7:55
So the output will be something like
7:57
this which is 21.24.
8:00
So it's super useful if you're
8:02
displaying prices, percentages or
8:04
measurements. So if you want two
8:07
decimals then you have to keep this
8:09
format in your mind.
8:13
And we also have one more case where
8:16
like for example now if you're using
8:18
Python latest version I really hope you
8:22
are uh you have access to something very
8:25
cooler which is fstrings.
8:28
So frings let your insert variables and
8:31
even expressions directly inside your
8:34
string like you don't even have to use
8:36
this dot format. So this is not at all
8:40
needed in the latest version. Let me
8:42
show you how.
8:52
Like for example, name equal to John
8:56
and age equal to 25
9:00
and message.
9:14
Hello.
9:18
And here you can just directly give name
9:24
and full stop
9:28
QR
9:31
Hold.
9:38
So just print
9:43
message.
9:46
Okay. So this is what you do. So here
9:49
what you're doing if you see the
9:51
difference. So dot format will actually
9:55
you are actually inserting the name and
9:58
age itself. But the actual difference is
10:00
you don't have to insert you don't have
10:02
to use dot for format here. You can just
10:05
use f here and inside the brackets you
10:08
can just give the names. So that is how
10:11
it will format the strings and you can
10:13
just print the message and let's try.
10:19
So the message is hello John you're 25
10:22
years old. So this is really simple and
10:24
easy and it will also optimize your code
10:27
if you check the difference. So it will
10:30
make it more you know easier to
10:32
understand. So always notice the f
10:35
before this opening quote which when you
10:38
are creating the string this actually
10:41
tells Python this is an f string. Then
10:44
whatever you put inside these brackets
10:47
gets evaluated and inserted directly
10:50
into the string using the variable name.
10:54
So name so hello name. Now, Python will
10:56
go here and it will insert John here and
10:59
you are age and it will go here and it
11:01
will insert age here years old.
11:06
So this is how it will print. We also
11:09
have to work on using expressions inside
11:12
the f string. So the real magic of
11:15
frstrings is that you can do
11:16
calculations and expressions right
11:19
inside the curly braces. You don't have
11:22
to write no extra lines of code which
11:25
are not at all needed.
11:28
So let me show you one good example.
11:31
So let's take a= 5
11:35
and b = 3
11:38
and result
11:41
equal to f
11:44
f
11:46
and the sum of
11:53
a
11:57
and
12:01
B
12:04
is
12:06
in the brackets itself A + B.
12:13
So now let's just print the result.
12:25
So the sum of five and three is 8. So
12:28
the sum of a is five and b is 3 and a +
12:32
b 5 + 3 is 8. So this is how it will
12:36
work. So python will calculate a + b
12:39
inside the f string and it will print.
12:42
So the sum of five and three is 8. So
12:45
isn't that awesome because it makes your
12:47
code cleaner, easier to read and way
12:50
less errorprone.
12:53
So you can also take a real world
12:55
example. So let's put it all together.
12:57
Whatever we have learned today, we are
13:00
going to keep all together in one thing.
13:03
So like for example
13:12
name we have learned input.
13:15
So let me just give
13:18
input.
13:24
What's your name?
13:30
And we are taking the user input here.
13:34
and age.
13:36
So age we need it in integer. So we are
13:38
converting the string to integer. Okay,
13:42
let's take let's make it easy for first
13:44
take the input from the user.
13:46
So how old are you?
13:54
And now we need this two integers. So
13:56
let's just give int
13:58
and int. Okay. And now print
14:04
here itself. Keep F and then
14:10
nice
14:12
to meet you.
14:16
And inside just give name
14:21
or you can give this
14:27
in
14:29
5 years
14:31
you will be
14:39
age + 5.
14:44
We'll stop. And now can you check
14:49
what's your name? Lucky.
14:53
How old are you? 25.
14:56
Nice to meet you Lucky. In 5 years
14:58
you'll be 30. So this is how it will
15:01
show you the output. So that's actually
15:05
fstrings in action making your
15:06
interactive program simple and elegant.
15:10
So I think you got me. So here we have
15:13
took the input from the user asking the
15:16
name what's your name and then we got
15:18
the age how old are you you got 25 and
15:21
now we have actually printed nice to
15:24
meet you and here it will substitute it
15:26
with the lucky and here you can check
15:29
the age plus five.
15:34
All right friends, so today we have
15:36
covered two powerful ways to format
15:38
strings in Python which is dot format
15:41
method
15:43
and we also worked with fst strings.
15:47
The best choice if you are on Python 3.6
15:51
the formatting strings is already there
15:54
in previous versions. It will work on
15:56
all modern Python versions
15:58
but fstrings it is the latest version.
16:02
With these, you'll make your programs
16:03
much more dynamic, readable, and
16:06
professional. If you enjoyed this video,
16:09
don't forget to like, subscribe, and hit
16:10
the bell icon so you'll never miss the
16:13
next episode. So, we have got lots more
16:16
awesome Python topics coming your way.
16:19
And thanks for coding with me.

