Delete last character printed in python 3.6 [duplicate] - python

This question already has answers here:
Delete last printed character python
(3 answers)
Closed 4 years ago.
I'm trying to delete the last character I've printed from the previous line in python, e.g if I do print ("hello world")
I will get hello world outputted, but I'm wondering after it's been outputted would there be a way to remove the d at the end of the output so it would now only say hello worl... in short is there a way to remove the end character of an already printed line?
Thanks

There is no way to perform operations on data that has been output since it is not stored in memory as a variable. You can however overwrite the location on the screen (if you are dealing with something like a terminal) that it is printed on to 'remove' the old data.
Here is a function you can add and call in your code to send text to a specific location on the terminal display:
import sys
def PrintLocate(row, column, text):
sys.stdout.write("\x1b7\x1b[%d;%df%s\x1b8" % (row, column, text))
sys.stdout.flush()
# Elsewhere
PrintLocate(13, 37, "Hello World")
PrintLocate(13, 37 + len("Hello Worl"), " ")

Related

How do I rewrite multiple console lines? [duplicate]

This question already has answers here:
How to overwrite the previous print to stdout?
(18 answers)
Closed 1 year ago.
I want to make a game in the python console, so I need to write out lines, and then re-write them. I started building up code for it, and came up with this:
import sys
while 1:
#I will calculate what to write here, and store it in display
display = ["thing", "other thing", "2nd other thing"]
#Move the writing start back to the beginning
for x in display: sys.stdout.write("\r")
#Write the contents of display
for x in display: sys.stdout.write(x + "\n")
However, the code does not erase the previously written text. It just repetitively prints the display list. How can I make it erase the text?
Edit:
Similar Answer How to overwrite the previous print to stdout in python?
Method:
You can do this by printing out as many whitespaces as you have characters on that line
However this would only be possible in a clean manner if you know the length of everything being printed.
For example if you know the length you can do the following
print("\r")
print(" " * length_of_line)
print("\r")
Otherwise if printing long line isn't a concern, you can adopt the brute-force method of print a load of whitespaces and hope it overwrites the whole line
print("\r")
print(" " * a_large_number)
print("\r")
You cannot use the "\r" separately from the print statement and I think you can only use it in this way with the print statement.
An implementation with the print statement would look like this:
for i in range(3): #No endless loop
display = ["thing", "other thing", "2nd other thing"]
for x in display: print(x, end="\r")
This would result in every line overwriting the previous one.
Also have a look here, there they also discuss different methods.

Misaligment while formatting with escape t "\t" [duplicate]

This question already has answers here:
Incorrect column alignment when printing table in Python using tab characters
(4 answers)
Closed 1 year ago.
I'm learning Python through "Python Projects for Beginners" by Connor Milliken. In the first project "Creating a Recipt Printing Program" there is this section
# creating a product and price for three itens
p1_name, p1_price = "Books", 49.95
p2_name, p2_price = "Computer", 579.99
p3_name, p3_price = "Monitor", 124.89
# create a print statement for each product
print("\t{}\t\t${}".format(p1_name.title(), p1_price))
print("\t{}\t\t${}".format(p2_name.title(), p2_price))
print("\t{}\t\t${}".format(p3_name.title(), p3_price))
The lines are equal but for the second line the price is misaligned as if it has another \t. The problem was the same in jupyter notebook and Atom + terminal. If you just delete one '\t' the problem is solved but you can't really understand what happened.
Don't think of a tab as inserting a specific number of spaces in the string (it doesn't). Instead, you are giving control over whoever displays the string, since they are the ones that decide where the tab stops are.
If you want precise control, use fixed-width padded format specifiers instead. For example,
print(" {:>10} {:>6}".format(p1_name.title(), p1_price))
This assumes that 10 characters is wide enough for any title and 6 characters is wide enough for any price.

Python 3: Reading string from file and define the same string in code working differently [duplicate]

This question already has answers here:
Process escape sequences in a string in Python
(8 answers)
Closed 4 years ago.
I have a text file like below
# 1.txt
who is the\u00a0winners\u00a0where\u00a0season result\u00a0is 7th
If I read a file and print it, it shows
>>> s = open("1.txt").read()
>>> print(s)
who is the\u00a0winners\u00a0where\u00a0season result\u00a0is 7th
However, If I do like below with the same string,
>> s = "who is the\u00a0winners\u00a0where\u00a0season result\u00a0is 7th"
>> print(s)
who is the winners where season result is 7th
I want to read a text file like "1.txt" and print it like the below one. I can not find how to do it. Please help me. Thanks.
\u00a0 is a non break space and is one character.
In your first example your are reading \u00a0 as 6 chars.
If you want to read a file with \u00a0s and interpret them as spaces, you would have to parse the file yourself and create spaces for each \u00a0.

How to print the value of a variable in multiple lines in python? [duplicate]

This question already has answers here:
A good way to make long strings wrap to newline?
(8 answers)
Wrap long lines in Python [duplicate]
(6 answers)
Closed 6 years ago.
I am writing the plot of a movie from OMDB API. I want to limit the plot value to be within 80 length in the output. I have searched but, could not found anything. The jsonvalues['Plot'] can contain more than 80 characters. I want to print its value in multiple lines.
import urllib
import json
name = raw_input('Enter the movie name >')
url = "http://www.omdbapi.com/?t="+name+"&r="+"json"
response = urllib.urlopen(url).read()
jsonvalues = json.loads(response)
if jsonvalues["Response"]=="True":
print jsonvalues["imdbRating"]
print 'The plot of the movie is: '+jsonvalues['Plot']
else:
print "The movie name was not found"
Check out if this is you wanted,
In [408]: import textwrap
In [409]: s = "This is a long line. "*15
In [410]: w = 75 # width
In [411]: print(textwrap.fill(s, w))
This is a long line. This is a long line. This is a long line. This is a
long line. This is a long line. This is a long line. This is a long line.
This is a long line. This is a long line. This is a long line. This is a
long line. This is a long line. This is a long line. This is a long line.
This is a long line.
There are all kinds of functions in textwrap module. Check them out.
textwrap module does it.
import textwrap #insert at top of code
print "\n".join(textwrap.wrap('The plot of the movie is: ' + jsonvalues['Plot'],80))

How do I make python type something one number or letter at a time, or on a delayed pace? [duplicate]

This question already has answers here:
How to print one character at a time on one line?
(4 answers)
Closed 8 years ago.
Ok this is going to be hard to ask/explain but bear with me.
Im trying to just make things look cool when I use the while True command.
What I am trying to do Is make it type stuff slower or one letter at a time.
For example, here is my code.
while True:
print ("010101010101010101010101010101")
print ("010101010101001010101010101010")
print ("010101010101010101010101010101")
When I do that it obviously rapidly repeats the commands I entered in the file.
I am aware there is the following,
import time
time.sleep(5)
But I wan't it to type it one at a time, not on a 5 second relay.
I hope you can understand what I am trying to ask. Thank you so much for helping me.
Here's one possibility:
import sys
import time
def cool_print(str):
for char in str:
sys.stdout.write(char)
sys.stdout.flush()
time.sleep(0.05) # Or whatever delay you'd like
print # One last print to make sure that you move to a new line
Then instead of print ("010101010101010101010101010101"), you'd use cool_print("010101010101010101010101010101").
It sounds like you want a delay between each actual character, so you need to call sleep in between each one:
import time
while True:
for binary_char in "10101010101010101":
time.sleep(5) # Replace this with a much smaller number, probably
print binary_char, # Remove trailing comma to print each character on new line

Categories

Resources