SoFunction
Updated on 2024-11-18

A super-detailed explanation of the many cases of Python input (single line, multiple lines and arrays)

mentioned python input, we first thought of xxx=input(), this is our most common and most commonly used, in fact, in some special input conditions, we need to be flexible with a variety of methods to solve a variety of input conditions, the following python input methods to do some introduction:

1. Diversification to realize various types of inputs

Let's start with some common pairings

(1) Enter an integer

# 123
in_1 = int(input())

(2) Enter two or more integers

# 12 24 48
in_1,in_2,in_3 = map(int,input().split())

(3) Enter a line of strings or individual characters

# helloPythonExE or A
in_1 = input()

(4) Input multiple strings or multiple characters

# ab bc cd or a b c
in_1,in_2,in_3 = input().split()

(4) Input a line in the form of an array of integers

# 1 2 3 4 5 6 7 8
in_1 = list(map(int,input().split()))

(5) Input a line in the form of an array of characters or an array of strings

# ab abd abcd ef efg fgh hijk or a b c d e f g h i
in_1 = list(input().split())

(6) Input a line in the form of an array that needs to be divided by a specific character

# ab-cd-ef-gh-ij-kl-mn
in_1 = list(input().split('-'))
# aaa*bbb*ccc*ddd*eee
in_2 = list(input().split('*'))

(7) List generator method to deposit a row of array form input

# aaa bbb ccc 111 123 456 sss
list_1 = [x for x in input().split(' ')]

That's about it, let's introduce multi-line input as well as input without specifying the number of lines (the following example of specifying the number of lines takes 5 lines as an example).

(1) Enter a string or character with a specified number of lines.

# aaaaa
# bbb
# cccc
# d
# eeee
for x in range(5):
    in_x = input()

(2) Enter the integer of the specified number of lines (in fact, just add an int to the above)

# 12
# 345
# 67
# 8
# 9
for x in range(5):
    in_x = int(input())

(3) enter the specified number of lines in the form of an array (as an example, integer elements, strings or characters only need to replace int in the map for str)

# 1 2 3 4 5
# 2 3 4 5 6
# 7 8 9 0 1
# 3 3 3 3 3
# 1 3 5 6 7
for x in range(5):
    in_x = list(map(int,input().split()))

(4) Enter a specified number of lines of strings or characters in a list generator.

# abcd
# 1234
# xyz
# 12abc
# cccc
in_1 = [input() for x in range(5)]
print(in_1)

(5) Input the specified number of lines of integers in a list generator.

# 12
# 345
# 6789
# 567
# 10
in_1 = [int(input()) for x in range(5)]
print(in_1)

(6) Input the specified number of rows in the form of an array in the form of a list generator (strings or arrays of characters just replace the int in the map with str)

# 123 456 789 1000
# 12 34 56 78
# 77 888 9999
# 1000 100 10 1
# 6 66 666 6666
in_1 = [list(map(int,input().split())) for x in range(5)]

(7) Inputs an unspecified number of lines and stops the input by ending with a specific input (ending with the input string '0000' as an example)

# 1111
# 2222
# 3333
# 4444
# 0000
while True:
    in_1 = input()
    if in_1 == '0000':
        break

(8) Entering an indeterminate number of lines and not knowing when the input will stop

# aaa
# bbbc
# ddd
# eee
# ............ How many more lines to enter is unknown
while True:
    try:
        in_1 = input()
    except:
        break

2. Standard type input ()

With our commonly used input() input method is different, () is python's standard input method, it is not like input as the need to deal with the end of the carriage return symbol, directly read a line all the way in, compared to input(), in a large number of input () more time-saving, useful, and multiple with still use it, the use of the use of the same as the above usage, just replace input() for (), it should be noted because it does not deal with line breaks, when using the need to deal with line breaks, manually deal with line breaks can use replace('\n',), you can use replace('\n',), (), (), and (), you can use (''n'',), and (). ), need to pay attention to because it does not deal with line breaks, the use of attention to the need to deal with line breaks, manually deal with line breaks can be used to replace ('\n','') and other methods, and is the need to guide package

(1) () read in a line

# hello readline
import sys 
in_1 = ()

summarize

This article on the Python input of a variety of super-detailed interpretation of the article is introduced to this, more related to Python single-line, multi-line and array input content please search for my previous posts or continue to browse the following related articles I hope you will support me in the future more!