6
10

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

Python3 標準入力基本

Last updated at Posted at 2019-02-06

備忘録的にpython3の標準入力をまとめてみました。

環境

virtualbox 5.2.26
vagrant 2.1.2
python 3.5.2
mac 10.14.2

1行取得

###文字列

input.txt
string
input.py
s = input()
output.py
string

###数値

input.txt
100
input.py
n = int(input())
output.py
100

###スペースで区切られた文字を文字列として

input.txt
hage hoge hige
input.py
s = input().rstrip().split()
output.py
['hage', 'hoge', 'hige']

###スペースで区切られた文字を数値として

input.txt
100 200 300
input.py
n = list(map(int, input().rstrip().split()))
output.py
[100, 200, 300]

###スペースで区切られた文字を数値に置き換え

input.txt
5 1 2 1 A 3 A
input.py
s = input().rstrip().split()
n = list(map(int, [i.replace('A', '5') for i in s]))
output.py
[5, 1, 2, 1, 5, 3, 5]

複数行取得

###指定回数分の行を数値として

input.txt
3
100 200 300
400 500 600
700 800 900
input.py
n = int(input())
nums = [list(map(int, input().rstrip().split())) for i in range(n)]
output.py
[[100, 200, 300,], [400, 500, 600], [700, 800, 900]]

###指定回数分の行を数値として(スペースで区切られたVer)

input.txt
3 4 5
1 2 3
4 5 6
7 8 9
input.py
Num, a, b = list(map(int, input().rstrip().split()))
res = [input().rstrip() for i in range(Num)]
output.py
[[1, 2, 3], [4, 5, 6], [7, 8, 9]]
6
10
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
6
10

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?