Python input to get multiple lines of text

Today I use input and want to get multiple rows of data.
It is found that \n is used as a newline character by default.
Then check the Internet: I
found that the writing was dead and I couldn't change it.
Insert picture description here
There is only this one parameter.
Print has an end parameter, but input does not.
Can't change it.

So I wrote a function instead.
If input wants to get multiple lines of text, you need to set a terminator (otherwise how does the system know that you have finished typing), the common ones are spaces or \n.

Think about it:
design an algorithm.

def get_char(end='end', end_char=''):
    data = end
    while 1:
        var = input()
        if var == str(end):
            break
        elif end_char != '' and var.find(end_char) != -1:
            var = var[0:var.find(end_char)]
            data = '{}\n{}'.format(data, var)
            break
        else:
            data = '{}\n{}'.format(data, var)
    return data.replace('{}\n'.format(end), '')

When reading a string, use this function get_char() instead of print()

Try to read 4 rows of data:
Insert picture description here
Then read it:Insert picture description here

(That is, you have to write an end every time, or get the character end_char to exit)

This is quite simple.

Guess you like

Origin blog.csdn.net/weixin_45642669/article/details/114199303