LoginSignup
0
2

More than 3 years have passed since last update.

【Python】標準入力について

Last updated at Posted at 2020-10-13

標準入力の取得について学んだ事を記載します

N 行の入力

入力
afd
agdp
hj

出力
afd
agdp
hj

for a in range(3):
    c = input()
    print(c)

2 つの整数の半角スペース区切りの入力

入力
6 90
出力
6
90

input_line = input().split()

for a in input_line:
    print(a)

2 行目で与えられる N 個の整数の入力


入力
6
56 78 39 26 4 553

出力
56 
78 
39 
26 
4 
553

構文
input_line = int(input())
#入力の1つ目を取得する

try:
    for a in range(input_line):
        v = input().split()
except EOFError:
    pass
#1.for文にて1つ目に取得した数値までをrangeで回して0~順に変数aに代入する
#2.for分の2行目にて2つ目の入力を取得し,split()で文字列を分割する
#3.1つ目に取得した数値分,繰り返す

for d in v:
    print(d)

try-except文を入れないとEOFError: EOF when reading a lineというエラーが出てしまう

2 組の整数の入力

入力
4
57 10
53 38
9  46
96 29

出力
57 10
53 38
9  46
96 29

構文
input_line = int(input())

for i in range(input_line):
    a, b = input().split()
# ここでa, bに1行目と2行目の値を分けている
    print(a, b)

整数の組からの選択(特定の組数目からの抽出)

入力
10
46 5
2 7
89 6
81 0
46 675
2 948
68 35
17 10
26 64
28 95

出力
26 64

構文
# if文にて出力
input_line = int(input())

for a in range(input_line):
    x, y = input().split()
    if a == 7: 
        print(x, y)


別解
# リストに追加して出力
myinput = []
input_line = int(input())

for a in range(input_line):
    x, y = input().split()
    myinput.append((x, y))

print(' '.join(myinput[7]))

入力には関係ないけど

import string
# stringモジュールは文字列を取得するモジュール

alphabets = string.ascii_uppercase
# ABCDEFGHIJKLMNOPQRSTUVWXYZ
# ascii_lowercaseはa~zまでを取得する
# ascii_uppercaseはA~Zまでを取得する
# digitsは数字を取得する

# X はインデックスの何番目?
print(alphabets.index("X") + 1)  # 24

随時更新

0
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
2