def send():
data = input()
print("Server:",data)
c.send(data.encode())
I'm making a messenger and want to have names before their messages. But I cant find a way to clear the input that they type before so messages start repeating. I tried using \r but it doesn't seem to work with inputs and I cant find modules that could clear the specific line.
You can give the input() function an argument which will be placed before the actual input.
def send():
data = input("Server:")
c.send(data.encode())
I think you want it to look like this:
bob: hey
msg> how are you?
When the user hits Enter, it should change to:
bob: hey
bob: how are you?
You can do this with ANSI escape sequences on supported terminals.
data = input("msg> ")
# when the user hits enter, the cursor moves down a line and to the first column
print("\033[A", end="") # move the cursor up a line
print("\033[K", end="") # erase the line
print("bob:", data) # print something new in the place of the input
Related
I am new to coding. And I would like to know if there's a way for input function to not print newline character after the value is entered. Something like print function's argument end. Is there any way?
Well, you can't make input() trigger by anything besides 'Enter' hit (other way may be using sys.stdin and retrieving character one-by-one until you receive some stop marker, but it's difficult both for programmer and for user, I suppose). As a workaround I can the suggest the following: if you can know the length of line written before + length of user input, then you can use some system codes to move cursor back to the end of previous line, discarding the printed newline:
print("This is first line.")
prompt = "Enter second: "
ans = input(prompt)
print(f"\033[A\033[{len(prompt)+len(ans)}C And the third.")
\033[A moves cursor one line up and \033[<N>C moves cursor N symbols right. The example code produces the following output:
This is first line.
Enter second: USER INPUT HERE And the third.
Also note that the newline character is not printed by your program, it's entered by user.
name=input('Enter your name : ')
print('hello',name,end='')
I know about the end function which is abov
I'm learning Python and for my homework I wrote a simple script that get a string from user like this one: aaaabbbbcccdde and transforms it to a4b4c3d2e1.
Now I've decided to get things more interesting and modified code for continuous input and output in realtime. So I need a possibility to enter symbols and get an output coded with that simple algorithm.
The only problem I've faced with I needed output without '\n' so all the coded symbols were printed consequently in one string e.g: a4b4c3d2e1
But in that case output symbols mixed with my input and eventually the script froze. Obviously I need Enter symbols for input on one string and output it on another string w/o line breaks.
So, could you tell me please is it possible without a lot of difficulties for newbie make up a code that would do something like this:
a - #here the string in shell where I'm always add any symbols
a4b4c3d2e1a4b4c3d2e1a4b4c3d2e1 - #here, on the next string the script continuously outputs results of coding without breaking the line.
import getch
cnt = 1
print('Enter any string:')
user1 = getch.getch()
while True:
buf = getch.getch()
if buf == user1:
cnt += 1
user1 = buf
else:
print(user1, cnt, sep='')
user1 = buf
cnt = 1
so this snippet outputs me something like this:
a4
s4
d4
f4
etc
And in all cases when I'm trying to add end='' to output print() the program sticks.
What is possible to do to get rid of that?
Thanks !
I don't really know the details but I can say that: when you add end='', the program don't freeze, but the output (stdout) does not refresh (maybe due to some optimisation ? I really don't know).
So what you want to do is to flush the output right after you print in it.
print(user1, cnt, sep='', end='')
sys.stdout.flush()
(It is actually a duplicate of How to flush output of print function? )
Is it possible to create scrolling text in the Python command line by repeatedly updating the same line of text with small time.sleep() delays?
I believe that the \b (backspace) character can effectively move the cursor backward over text already written. I thought that combining this with end="" parameter in Python 3 might allow subsequent print commands to update a previous print command. With careful tracking of the length of the text and insertion of backspace and other characters it should be possible to create static lines of text that animate in place.
Unfortunately none of this works and even the \b character seems to do nothing:
word = input("Type something-> ")
print(word+"\b\b\bHello", end="")
print("New text")
Anyone got any ideas?
Many thanks,
Kw
Maybe you need carriage return, or \r. This takes you to the beginning of the line. It is the same effect as in a physical typewriter when you move your carriage to the beginning and overwrite whatever is there.
If you use this:
print("Hello", end=" ")
print("world")
The output will be:
Hello World
But if you use:
print("Hello", end="\r")
print("world")
The output will be only:
world
I'm making a basic utility in Python 3 where the user inputs a command and gets feedback printed out into the console. When entering data using the input() or sys.stdin.readline() functions this is what the command-line session may look like (including \r and \n characters)
1. What is your name:\n
2. <user input>\n
3. Your name is <variable>.\n
But, I would like to display a \r character after the user hits enter instead of the \n character, as shown on line 2. After the user had typed everything in and hit enter it would look like this
1. What is your name:\n
2. Your name is <variable>.\n
(because line 2 would have a \r character after the entered data, returning the cursur back to the far left)
Does anybody know of a way I might accomplish this?
Well, I discovered this method although I am almost cirtain that the msvcrt module is for Windows only.
import msvcrt
import sys
def msgInput(prompt):
print(prompt, end='')
data= b''
while True:
char= msvcrt.getch()
if char != b'\r':
print(char.decode(), end='')
sys.stdout.flush()
data= data+char
else:
print('\r', end='')
break
return data.decode()
If anybody knows of any cross-platform methods, please share.
Update - Unfortunately this method has many limitations, such as the user cannot navigate the entered text with the arrow keys.
Well, I believe I found the solution you wanted:
strng=input("Enter String - ")
Where strng is just a variable to hold the input response. This will return the string in the interpreter Enter String - (without any quote) and allow you to save the response in the variable strng.
I'm using raw_input() to storing a message inside a variable. So I can't press enter for a carriage return/new line to start a new paragraph. Right now if I press enter it will just proceed to the next portion of my program.
I already tried something like this:
>>> message = raw_input("Message: ")
Message: Hello Sir, \n It's great that..
>>> message
"Hello Sir, \\n It's great that.."
>>>
It didn't worked, and I also tried enclosing it in single and double quotes, which also didn't worked.
I understand that there are other ways of doing this, like using wxpython or tkinter, but I want to keep it strictly console. Is this possible?
Can you use the sys module? This will do the trick if you want. Just hit Ctrl-D to end it.
import sys
message = sys.stdin.readlines()
Otherwise, this answers your question: Python raw_input ignore newline