How to enter text between two lines?

When i have this code

input('Write your name: ')

You can write your name after this line.

>>>Write your name: My name

But how to get something at the end of input?

>>>Write your name: (Name)
>>>Write your name: (Name Name)
>>>Write your name: (Name Name Name)

So, no matter what you write, the ") symbol appears when you write?

+6
source share
2 answers

I would suggest the following implementation, which uses msvcrtone char to read every time and acts on it (processing Enterand Backspace, if it occurs):

import msvcrt

def input_between_strings (s, t):
    res = ''
    while True:
        print(s, res, t, sep = '', end = ' \r')
        curr = msvcrt.getch()[0]
        if curr == 13:
            print()
            return res
        elif curr == 8:
            res = res[:-1]
        else:
            res += chr(curr)

In your case, you can call it like

result = input_between_strings('>>> Write your name: (', ')')
+2
source

, . , , Enter, . , , . ... , , stdin stdout.

-1

All Articles