0
0

【Python】標準入力を受け取る方法(単数,複数)

Last updated at Posted at 2024-05-07

はじめに

個人的なメモ、調べたことをアウトプットしています。

間違いあればご指摘の程いただけますと幸いです。

単数の標準入力

文字列

input() にて標準入力を受け取る。

input_line = input()
print(input_line)

#-----result--------
>>> apple
# apple

整数

int()input() の標準入力を整数型にフォーマットし取得。

input_line = int(input())
print(input_line)

#-----result--------
>>> 24
# 24

複数の標準入力

文字列

input() の標準入力をsplit() で半角スペースの位置で区切って取得。

a,b = input().split()
print(a,b)

#-----result--------
>>> リンゴ ゴリラ
# リンゴ ゴリラ

カンマ区切りを取得するにはsplit(",")で取得。

a,b = input().split()
print(a,b)

#-----result--------
>>> リンゴ,ゴリラ
# リンゴ ゴリラ

整数

map

input() の標準入力をsplit()で半角スペースの位置で区切って取得。

map()で各要素のマッピングを行う。

n, x, y = map(int, input().split())
print(n,x,y)

#-----result--------
>>> 5 4 2
# 5 4 2

for

input() の標準入力をsplit() で半角スペースの位置で区切って取得。

int(x) for in でループして整数型にフォーマット。

a,b=(int(x) for x in input().split())(
print(a)
print(b)

#-----result--------
>>> 8 9
# 8
# 9

まとめ

入力パターンや取得方法は、まだまだパターンがあると思います。
まとめ次第、記事を書くかこの記事に追記していこうかと。

0
0
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
0