This video covers how to use the formatted string shortcut method of combining variables of multiple data types in one string output in the python programming language!
Fstrings allow you to write out a full string without the standard method of concatenating using plus signs (+) with the str() string conversion tool on every variable or data type that isn't already in string format! By entering an f before the first quotation mark you signal that anything inside curly brackets {} inside the quotation marks is a variable but should be converted to a string for reporting!
This is a great beginner tutorial and if you're looking to use beginner python skills in creating applications, games and fully functionality utilities be sure to check out the rest of the channel! https://www.youtube.com/channel/UCV5Ab39YnXvTZ6Grar9URxQ
Let us know with any questions, comments, concerns or ideas for a future video!
Thanks for watching and be sure to like and subscribe for more great content!
hello and welcome back to the channel
and today's video is going to be
learning to use python's f string
function to simplify formatting string
concatenation with variables
[Music]
so in python
if you're at least moderately familiar
with it you know that uh when you print
out a statement onto the screen it needs
to come in the format of text
if there's any
any kind of like including a variable
with a statement so what i mean by that
just really briefly is if you want to
print a number that's in the format of a
number
that can be the only thing in your print
statement so if i want to print 12 i can
do that but if i want to print
hey
12
then what this is going to do is is this
is going to error out because now you
have a text and you have number and so
uh the easiest way to concatenate two
things which is just the fancy code word
for sticking them together if you're
familiar with excel you've probably seen
concatenation
is just the plus sign
in python so you would do hey plus 12
but then it tells you even in the error
statement depending on what id you're
using
you can only concatenate strings not
integers
so what you have to do is you have to
use the str function which is python's
built-in string converter and you put
whatever you want inside of the
parentheses of str and now at least we
have valid concatenation it'll say hey
12.
okay so that is um
that is kind of how to use the str
function that's basic python
concatenation
but let's say you wanted to use some
variables in there so we'll make my
string and we'll just have the text
inside of that variable say example
string
well right away you'll see that you can
concatenate using this very basic method
you can say hey
plus my string and this is going to go
no problem
but if we had a number as well so we'll
make my number
and we'll pick a random number like 420
and we'll say hey
uh my number okay and what we're gonna
find is we're getting the same error as
when we put just a 12 in there because
it's an integer so we use that str again
just like before
and now it's valid
but there's a better way because when
you want to use various different types
of variables so let's get one more in
here let's make a boolean and these are
really the three most common types of
python variable now we have a string a
number and a boolean um and let's just
set it equal to true
because a boolean is true false you
should know that
and uh let's say
um we wanted to have hey plus number
plus
uh my string and we want all of these in
the same
um in the same thing what you'll see it
looks pretty goofy there's no space
between
the string and the number so we'd have
to put a space
just we'd have to have a whole nother
plus plus two quotes plus just a space
to have a space in there
and just to get what we wanted um now
the thing that python 3.0 and onward um
added which if you're using an older
version of python
um you can do a like string where at the
end you do dot format um it's it's an
older method of doing it i would just
look for an example on stack overflow or
something if you're using python 2.
if you have a reason to use python 2
though you probably know enough about
python that like why are you watching
this video
the new one that um if you're
downloading python from the internet
today you're getting a version of python
3 has something in it called an f string
and all you have to do the f stands for
format i'm pretty sure all you have to
do is put an f in before your quotations
and i think i've talked about this
before in python but you can use a
single quote or a double quote they're
totally equivalent i know in c sharp if
you use the single quotes it's like a
character string where it can only be
one
there's no difference in python
so you put an f before it and now
instead of that str my number you're
saying everything inside of this text
that i type in and variables that i put
inside of these curly brackets are going
to get formatted into a string
so let's put my number inside those
curly brackets and then we can just keep
typing so whereas up here we had to have
hey stop the quotations use a plus sign
str format my number then another plus
sign then a space and then add my string
we just say hey my number hit the space
key and then
my string
and just doing this is going to give us
you'll see when you run it it's exactly
the same but uh
where this really comes in handy is like
say you have some
function going on where you pull in five
or six inputs and all of those are going
to get reported in your output statement
let's just add a third variable into
this so we need another plus another
space
and then another plus and then another
str format and then you would put my
boolean in there well in here
that whole thing we just added is just
the space key the curly brackets and
then my boolean
okay and we're not even actually doing
anything particularly productive with
this formatting like if we want to say
hey
my number is my number and then a comma
and then my string is and then my string
and then and my boolean is
okay every place that we
had that uh just space in there we would
put our text in there and you have to
make sure spacing is correct and you'll
run it and you'll see like oh two
variables end up like jammed up against
text because you didn't put the right
spaces
an f string is just a full
formatted uh function where you get to
see your string as it's going to look
you don't have this goofy str
every time there's a non-string variable
so this really is quite a bit better
and
one step further even inside these curly
brackets you can do
function
level things like you could instead of
my number you could say my number minus
10 because
this code doesn't process until you run
it so like if my number is 420 you could
say my number minus
i don't know it's random number
um and then you could say my string is
and you could use things like the
string conversions which there's tons of
ways to play around with strings there's
a ton of literature out there on it but
this
one i just put in upper is going to
convert our string to uppercase
and you'll see when we run it
it does that math it does that
subtraction that we put in the curly
brackets and it does the upper
conversion inside the curly brackets
that's pretty cool
so it really is the f string is a much
better alternative to traditional
concatenation
in terms of combining text with
variables in output strings so i hope
you found this useful if you did feel
free to check out the channel for tons
of other great
tutorials and content like this
make sure to leave a like on the video
subscribe to the channel it helps me out
a ton as we're a fairly new youtube
channel and if anything in this video
confused you or you want to see anything
in particular in the future just let me
know about in the comments below i'll
get back to you as soon as possible and
i really appreciate you checking out the
channel as always good luck with your
code and thanks for watching thanks bye