How to Overwrite Previous Printed Line in Python | Print Without Newline | Carriage Return

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
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   and then I'm gonna print a few things normally, so "hello" and time.sleep two seconds, print "I am Fabio", time.sleep and then, last one So, let's just have a look at how python  does that normally without changing anything   so I'm going to save the file, open the  terminal and then we're gonna run python   overwrite lines. So let's  have a look, "hello I am Fabio welcome to my channel", so this is actually the  default way. So let's clear and keep going so If you want to actually show these messages,  without actually changing line, so actually   overwriting the previous one, you need to use the  carriage return. So basically the carriage return   moves the cursor at the beginning of the line  so that the new string will be printed from the   beginning of the line, overwriting the previous  one. So the carriage return is this character,   this alright. So what's gonna happen here  is that python actually prints the hello   and then as there's this carriage return  the cursor will move from the end of hello   to the beginning of the string, so it's gonna  move here and then it's gonna print "I am Fabio"   overwriting the "hello", the same thing down  here. So let's have a look save the file and then I'm going to run this So as you can see it's not 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 and the line feed 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. So the default one is actually  this one, so we're gonna delete that and leave   an empty string as value, 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 that, let's have a look. As you can see, we just have the last one  because all the others have been overwritten,   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.  So I'm gonna just comment these out,   I'm gonna write, I'm actually going to copy this uncomment it and change a few things, so do this.  So now I'm gonna make this longer, so "Hello I am   Fabio", this a little bit  shorter like "welcome" and then   even shorter "here". S o let's  have a look at how it works pull up the terminal. So I'm going to  clear this and let's see what happens 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,   then it writes the string but the other string  is sort of beneath. 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, because   the cursor is actually here 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" is not going to be overwritten,   so as you can see here "ome I amFabio". 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. So now the actual string   is this long, 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 As you can see now it's working properly and how  it's supposed to work, so you can actually do   a thing like that I'm gonna  just copy and paste them Down here and comment these out. You can actually  add spaces in a different way so you can do like,   spaces, and then do something like, times 10,  so 10 spaces and then you can add them at the   end of the string so you can delete this and  you can do like, plus spaces and the same here,   you can do whatever you want to do, but I  mean, this looks a little bit better to me,   it's just my personal preference,  so let's see if it still works let's see Perfect! Really important thing to know is that  this method works only if the terminals width is   greater than the length of the string, 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 because the cursor is like,   let's say on the second line and the new string  will overwrite only the line and not the one   above. So maybe if I show you an example you'll  understand that better 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, so I'm actually  making the strings really really long   so they are actually gonna wrap, I'm going  to save the file, so let's see what happens.   So I'm gonna just narrow the terminal  like that, let's try that out Here what happens is that, this is the first  line, maybe the first line is like up to here   but then the "welcome" has overwritten  the third line so actually the cursor   probably was here 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. 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. Hopefully that makes sense,  so be careful when you are actually dealing with   things like that because you need to have the  terminal's width greater than the length of   the string 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,   so the line is gonna be the same every time.  So hopefully that makes sense 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, so let's comment this out   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, don't  know like, 100000 yeah and then I'm going to print carriage return then, "format", then I'm going to   use the n, the number, "n" 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'm  gonna clear 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 carriage return.   So as you can see I just wrote the carriage  return here but you could have written the   carriage return like here, at the end of the  string because: it prints a line and then at   the end it adds the carriage return so the cursor  goes back to the beginning of the line and then   it prints another string and then again the  carriage return, so either way actually works I'm going to save that, so I'm going to just  close everything as usual and that's it guys
Info
Channel: Fabio Musanni - Programming Channel
Views: 22,218
Rating: undefined out of 5
Keywords: python, python3, programming, coding, tutorial, education, carriage return, carriage return in python, python carriage return, python print in same line, python print on same line, print on same line python, python print on the same line, python print overwrite same line, printing in same line in python, python print on same line without space, print in the same line python 3, printing without newline, print without newline
Id: LXV53NKfKQI
Channel Id: undefined
Length: 12min 14sec (734 seconds)
Published: Sat Jan 09 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.