sometimes you just want to update the
same line with new text without going
down one line in today's video we're
gonna have a look at how you can do that
alright guys so i'm gonna start
by importing time
which we're gonna use all right
and then i'm gonna print a few things
normally right so
hello
and time
sleep
two seconds print
oh yeah
w
time dot sleep
and then
all right so let's just have a look at
how python does that normally right
without changing anything so i'm going
to save the file
open the terminal
and then we're gonna run
python
right lines all right so let's have a
look
hello
i am
fabio welcome to my channel right so
this is actually the default way all
right so let's clear
and
keep going
so
if you want to actually show these
messages right without actually changing
line right so actually overwriting the
previous one you need to use the
carriage return so basically the cards
return moves the cursor at the beginning
of the line so that the new string will
be printed from the beginning of the
line right overwriting the previous one
all right so the carriage return is this
character right
this
all right
so what's gonna happen here is that
python actually prints the hello and
then
as does this carriage return
the cursor will move
from the end of hello to the beginning
all right of the string so it's gonna
move here right and then it's gonna
print i am fabio
overwriting the hello the same thing
down here all right
so let's have a look
save the file
and then i'm going to run
this
so as you can see it's now working so
why isn't it working
well because print has an argument a
keyword argument which tells python what
to add at the end of each string printed
the default one is the line feed all
right and the line fit is like hitting
enter so the cursor goes down one line
automatically so we need to fix that
so to fix that we actually need to
change that end
like that
like that
and like that
so basically here we are telling python
that we don't want anything printed
after the string all right so the
default one is actually this one
all right so we're gonna delete that and
leave an empty string as value all right
so it's not gonna actually print
anything so i'm gonna save the file and
see
how it works
so i'm going to leave it like
let's have a look
as you can see
we just have the last one
because all the others have been
overwritten all right perfect there is
still a problem when the strings that
come after are shorter than the previous
one
so let's have a look at what i mean all
right so
i'm gonna just
comment this out
all right i'm gonna
write
i'm actually going to copy this
and comment it and change a few things
so
do this
all right so now i'm gonna make this
longer all right so hello
i am
yo
this is a little bit shorter like
welcome
and then
even shorter here
all right so let's have a look at how it
works
put up the terminal all right so i'm
going to clear this and let's see what
happens
right
it doesn't look right to me
so why is this happening
so basically when the cursor moves from
the end of the line to the beginning of
the line right
then it writes the string but the other
string is sort of beneath that right
so
for example welcome
has this length
so one two three four five six seven
characters
that means that
seven characters
are going to be overwritten
but then the remaining part is gonna
stay there all right because the cursor
is actually here
right and then there's the welcome here
and here has only four characters that
means that
welk
is going to be overwritten
but then the ome it's not going to be
overwritten right so as you can see here
ome
i am fabio right
so how can we actually fix this
there's a really simple way so i'm going
to show that to you
right now so
you just need to add spaces at the end
of the actual string so if you do
something like this
just random
number of spaces all right so now the
actual string is this long all right
so this is definitely longer than this
so all the spaces here are gonna
overwrite
the remaining part of the string and the
same down here so let's have a look at
how it works
all right
as you can see now it's working properly
and how it's supposed to work right
so
you can actually do
a thing like that i'm gonna just copy
and paste
down them
and comment this out
okay
you can actually add spaces in a
different way so you can do like
spaces and then do something like
you know times 10 all right so 10 spaces
and then you can add them at the end of
the string so you can delete this
you can do like plus
spaces
and the same here
you can do whatever you want to do
well i mean this looks a little bit
better to me
it's just my
personal preference all right so
let's see if
it still works
let's see
all right perfect
really important thing to know is that
this method works
only if the terminals width is greater
than the length of the string
right otherwise if the terminal has the
wrap text enabled
which is like a setting
the string is going to be
cut in two or more lines and it won't
work properly all right because because
it's like
let's say on the second line
and the new string
will overwrite only the line and not the
one above
right so
maybe if i show you an example you'll
understand a bit so that's what i'm
gonna do
so let's clear this up
so let's make this string really really
long i'm just gonna
add like
things like that you know just
to write something right so i'm actually
making the strings really really long
so they actually gonna wrap
save the file so let's see what happens
so i'm gonna
just narrow
the terminal right like that
let's try that out
all right
here what happens is that
this is the first line
all right
maybe the first line is like
up to here okay but then the welcome
has overwritten
the third line so actually the cursor
probably was here right and then it
started from the beginning of this line
and then overwrote this line with the
welcome and then it went down here this
is actually the welcome string but then
the cursor was here and then it went
here and started writing here like that
and then all these spaces
right
so it's not actually working properly
because the cursor wasn't on the same
line right
so the cursor needs to be on the same
line to work otherwise it'll start from
this line and then start from the
beginning of this line not actually this
line all right hopefully that makes
sense all right so be careful when you
actually
dealing with
things like that because you need to
have the terminals width
greater than the length of the string
all right or maybe you need to
make sure that the terminal doesn't have
the wrap text enabled and then the thing
is gonna work properly because actually
the text won't wrap all right so the
line is gonna be the same every time all
right so hopefully that makes sense
all right so i'm just gonna
do this i don't really like that
i just did it
to show you
and now i just want to actually have a
look at another little example all right
so let's comment this out
all right
and then let's have a look at
i don't know like
i've got a number
and i want to print all the numbers
up to you know like 100
000
yeah
and then i'm going to print
character return
that
format
then i'm going to
use the n the number
and
like that
then here i'm going to actually add one
to the number
save it and let's see
what we've got
i've got to clear this
let's have a look
so this is actually a little example to
show you how you can use things like
that so you can actually count things
and stuff like that so that's a way you
can use the cards return
all right
so as you can see i just
wrote the cards return here but you
could have written the cards returned
like here right at the end of the string
because
it prints a line and then at the end it
adds
the carriage return
and then so the cursor goes back to the
beginning of the line and then it prints
another string and then again the
character turn so either way actually
works
i'm going to save that
so all right i'm going to just close
everything as usual
and
that's it guys