hey everybody how's it going in this
Python video we're going to be looking
at what is the difference between STR
and re PR now this is a question that I
see that gets asked a lot but if you go
and try to test this out on your own or
try to figure it out by yourself
sometimes the answer isn't extremely
obvious so for example I have a little
test file here so if I save and run this
you can see that I have a variable a
that's equal to a list of 1 2 3 4 and
variable B that is a sample string and
if I print out STR and re PR of a down
here they are both exactly the same
thing of a list and if I print out STR
of B and re PR of B which is a string
you can see that they're very similar
this one doesn't have quotes around it
and this one does have quotes around it
so just from these simple tests it's not
extremely obvious as to what the
difference between these are so first of
all let's go ahead and take a look at
the Python documentation for these now I
have this blown up really large so that
you guys can so that you guys can read
it so within the Python documentation
this is for STR it says that it returns
a string containing a nicely printable
representation of an object for Strings
this returns the string itself the
difference with RP re PR is that STR
does not always attempt to return a
string that is acceptable to eval and
its goal is to return a printable string
so let's take a look at what our APR is
ok so it says that it returns a string
containing a printable representation of
an object this is the same value yielded
by conversions reverse quotes it is
sometimes useful to be able to access
this operation as an ordinary function
for many types this function makes an
attempt to return a string that would
yield an object with the same value when
pass to eval
so I think that's enough for that so at
even reading through the documentation
doesn't may make it extremely clear as
to what the difference between these are
especially if you don't know what this
eval is now if you dig deeper and you
start looking at sites like Stack
Overflow and things like that to answer
this question then you'll see kind of
the consensus to this answer is that the
goal of our EPR is to be unambiguous and
the goal of STR is to be readable so
what does that mean exactly well let me
show you an example of when something
needs to be unambiguous and when
something needs to be readable so I have
a terminal pulled up here and I already
have a Python interpreter started and
I've already made a couple of variables
here so I have variable a and I have
variable B and without knowing what they
are let me print out these variables as
strings so if I do STR a and you can see
that it looks like a date 2015 and then
it's got this little tag on the end plus
0 looks like a time or a time zone or
something like that so let me do STR B
and so it's the exact same thing so on
the surface a and B are the same if you
just look at the STR or what's returned
by STR now if we look back here at what
we have written out here
the goal of STR is to be readable and it
is very readable it looks like a date
you can tell that this is probably the
year the month the day and then hour
minute second things like that now the
goal of our EPR is to be unambiguous so
let's run these same values through our
EPR so if I ran our EPR a you can see
okay this is a 8th time and then as the
year the month and then you can see the
time zone info here is UTC if I do the
same thing to B then it's just a string
so that's what it means to be
unambiguous whenever we ran
our EPR on a and we can see that it's a
date time and what it meant in the
Python documentation about being able
able to pass something to eval pretty
much just means that now not all of them
return values like this but pretty much
it means that you should be able to put
this in to Python and run it as a
command so eight times a time I should
be able to copy this and paste it down
here in the interpreter and run it now
in that case I got an error because the
TZ info actually needs to be itz at UTC
or something like that really you can
see that this line here on let me go
ahead and clear the screen and rerun
these again so you can see that the at
this line here it looks more like a
Python command than this one up here
this one's meant to be readable and this
one is meant to be unambiguous so to
kind of drive this point home let me
bring up the file that does the same
thing as what I just had there in the
interpreter and just ignore these print
statements here I'm just doing a cheap
way of adding in new lines but you can
see here that I have a is equal to
daytime daytime UTC now things like that
and then be I just set equal to the
string representation of a now whenever
I print out the STR values of these
variables you can see that they look the
same just like they did in the terminal
window I had pulled up and the our EPR
values here you can see that one is
daytime daytime and the other one is
just the string representation of that
variable so really a good way to think
of this is that the representation the
our EPR is really used for is is really
meant for developers like if I'm
debugging something and something's not
working the way that it's supposed to
you know this might be a good example
maybe you get a value back from a
database that's a date time and you
forget to convert it to a date time and
it's just a string well if you just
print out
string you're not going to be able to
tell that that's a string is just going
to look like these values down here it's
going to be the STR value is just going
to look like this they're going to be
the same but if you print out the re PR
value of it then you can obviously see
that it's either a string or that it's a
date-time so that's meant for developers
and if you're going to show if you want
something to be user-friendly for
somebody who's a non developer then you
want to use STR because someone who's
less familiar with programming they
don't want to see this date/time
date/time TZ info stuff like that it's
going to be confusing to them this is a
more user friendly version of that so I
hope that made sense to you guys I hope
that it kind of cleared up when you
would use one over the other so but if
you guys do have any questions you know
just ask in the comment section below
and be sure to subscribe for future tips
and tutorials and thank you guys for
watching