言語バージョン 3.9.0

公開日 2020年11月22日 JST

更新日 2021年1月14日 JST

コード例の出力としてprint関数を使用しています。

使用方法がわからない場合は以下のページを参照してからコード例を確認してください。

print関数の使用方法

input

このページでは豊富な例を用いてPythonのinputの使い方を学ぶことができます。

inputはPythonの組み込み関数です。
標準入力先(一般的にはキーボード)から1行分だけ入力待ちをし、受け取った値を末尾の改行を除いたstr型のオブジェクト(文字列)として返します。 引数に値を指定することで、入力待ちする前に表示する文字列を指定することができます。

以降このページでは標準入力先をキーボード、出力先をターミナル(もしくはコマンドプロンプトなどのCUIツール)とします。

TL;DR

基本

# inputを使用するとターミナルで入力待ち状態になる
input1 = input()
# ターミナルで'python'と入力してEnter
==> python (ターミナルでのinputに対する入力)
print('input: ' + input1)
==> input: python
# input()の戻り値はstr型
print(type(input1))
==> <class 'str'>



# inputに引数を与えると入力前にターミナルに表示される
input2 = input('Type number: ')
# ターミナルで'100'を入力してEnter
==> Type number: 100 (ターミナルでのinputに対する出力と入力)
print('$' + input2)
==> $100
# 数値を入力してもinput()の戻り値はstr型
print(type(input2))
==> <class 'str'>




input3 = input('Type something: ')
# ターミナルで何も入力せずにEnter
==> Type something: (ターミナルでのinputに対する出力、入力なし)
# input3には空文字''が代入されるので条件式でFalseと判定される
if input3:
    print(input3)
else:
    print('Nothing Typed')
==> Nothing Typed




input4 = input('Type something: ')
# ターミナルで'Python'と入力しEnter
==> Type something: Python (ターミナルでのinputに対する出力と入力)
if input4:
    print('input: ' + input4)
else:
    print('Nothing Typed')
==> input: Python

関連情報:ifの使用方法

文字列以外への入力値の変換

# inputの戻り値はstr
input1 = input('type number: ')
# '100'を入力してEnter
==> type number: 100 (ターミナルでのinputに対する出力と入力)
# 以下の表現では数値計算は不可能
print(input1 * 2)
==> 100100
print(type(input1))
==> <class 'str'>




input2 = input('type number: ')
# '100'を入力してEnter
==> type number: 100 (ターミナルでのinputに対する出力と入力)
# intクラスのコンストラクトを使用してint型に変換して計算
converted_input2 = int(input2)
print(converted_input2 * 2)
==> 200
print(type(converted_input2))
==> <class 'int'>




# floatの場合
input3 = input('type number: ')
# '3.14'を入力してEnter
==> type number: 3.14 (ターミナルでのinputに対する出力と入力)
# floatクラスのコンストラクトを使用してfloatに変換して計算
converted_input3 = float(input3)
print(2 * converted_input3 * 6)
==> 37.68
print(type(converted_input3))
==> <class 'float'>




# boolの場合1
input4 = input('type something: ')
# 'python'を入力してEnter
==> type something: python (ターミナルでのinputに対する出力と入力)
# boolクラスのコンストラクトを使用してboolに変換して計算
converted_input4 = bool(input4)
print(input4)
==> python
print(converted_input4)
==> True
print(type(converted_input4))
==> <class 'bool'>




# boolの場合2
input5 = input('type something: ')
# 何も入力せずEnter
==> type something: (ターミナルでのinputに対する出力、入力なし)
converted_input5 = bool(input5)
print(input5)
==>
print(converted_input5)
==> False
print(type(converted_input5))
==> <class 'bool'>




# 変換処理時のエラーハンドリング:変換失敗例
try:
    input6 = input('type number: ')
    # 'python'と入力してEnter
    ==> type number: python (ターミナルでのinputに対する出力と入力)
    # 入力値が数値に変換できないのでValueErrorが発生
    converted_input6 = int(input6)
except ValueError:
    print('NOT number:', input6)
else:
    print(converted_input6)
==> NOT number: python




# 変換処理時のエラーハンドリング:変換成功例
try:
    input7 = input('type number: ')
    # '500'を入力してEnter
    ==> type number: 500 (ターミナルでのinputに対する出力と入力)
    converted_input7 = int(input7)
except ValueError:
    print('NOT number:', input7)
else:
    print(converted_input7)
==> 500

関連情報:type(組み込み関数)の使用方法

関連情報:例外処理(try,except,else,finally)の使用方法

その他

input1 = input('type white-spaced separated values: ')
# 'python java kotlin'と入力してEnter
==> type white-spaced separated values: python java kotlin (ターミナルでのinputに対する出力と入力)
print(input1)
==> python java kotlin
# strクラスのsplitメソッドを使用してlistを作成
list1 = input1.split(' ')
print(list1)
==> ['python', 'java', 'kotlin']




input2 = input('type comma separated values: ')
# 'apple,banana,kiwi'と入力してEnter
==> type comma separated values: apple,banana,kiwi (ターミナルでのinputに対する出力と入力)
print(input2)
==> apple,banana,kiwi
# strクラスのsplitメソッドを使用してlistを作成
list2 = input2.split(',')
print(list2)
==> ['apple', 'banana', 'kiwi']




# whileとセイウチ演算子を使用して入力がある限り処理を継続
# 条件文がFalseと判定されるまでループ継続され、
# 今回は空文字''が入力されるまで継続
counter = 1
# 1回目に'python'、2回目に'java'、3回目は何も入力せずにEnter
while input3 := input(f'Loop {counter}: '):
    print(str(counter) + ': ' + input3)
    counter += 1
==> Loop 1: python (ターミナルでのinputに対する出力と入力)
==> 1: python
==> Loop 2: java (ターミナルでのinputに対する出力と入力)
==> 2: java
==> Loop 3: (ターミナルでのinputに対する出力、入力なし)




# whileとlistの併用
list3 = []
# 1回目に'py'、2回目に'js'、3回目に'ts'、4回目は何も入力せずにEnter
while input4 := input('type anything: '):
    list3.append(input4)
==> type anything: py (ターミナルでのinputに対する出力と入力)
==> type anything: js (ターミナルでのinputに対する出力と入力)
==> type anything: ts (ターミナルでのinputに対する出力と入力)
==> type anything: (ターミナルでのinputに対する出力、入力なし)
print(list3)
==> ['py', 'js', 'ts']

関連情報:split(文字列)の使用方法

関連情報:whileの使用方法

関連情報:append(リスト)の使用方法

解説

基本

inputはキーボードから1行分の入力を受け付けます。

使用したコードの位置で処理が1度止まり、 キーボードから1行分の入力待ちをした後に末尾の改行を除いたstrオブジェクト(文字列)を戻り値(返り値)として返します。 100のような数値であっても文字列として値が返ってくるので注意してください。

また、引数に入力待ちの前にターミナルに出力する文字列を指定することが可能で、CUIプログラムを作成する際などに使用されます。

input1 = input()
# ターミナルで'python'と入力してEnter
==> python (ターミナルでのinputに対する入力)
print('input: ' + input1)
==> input: python
print(type(input1))
==> <class 'str'>




input2 = input('type anything: ')
# ターミナルで'999'と入力してEnter
==> type anything: 999 (ターミナルでのinputに対する出力と入力)
print('$' + input2)
==> $999
print(type(input2))
==> <class 'str'>




# ターミナルで何も入力せずEnter
if input3 := input('type anything: '):
    print('input: ' + input3)
else:
    print("nothing typed")
==> type anything: (ターミナルでのinputに対する出力)
==> nothing typed

関連情報:type(組み込み関数)の使用方法

関連情報:ifの使用方法

文字列以外への入力値の変換

inputの戻り値はstrオブジェクトです。
そのため、四則演算などをそのまま行おうとするとうまくいきません。
strオブジェクトから使用目的に沿ったオブジェクトに変換する必要があり、 intクラスやfloatクラスなどのコンストラクタを利用します。

しかし、コンストラクタの値として正しくない形式のデータを与えるとValueErrorなどの エラーが変換段階で発生してしまいます。
そのため、実用的には変換処理は例外処理(tryexceptなど)と併用して利用することを推奨します。

input1 = input('type number: ')
# '15'を入力してEnter
==> type number: 15 (ターミナルでのinputに対する出力と入力)
print(input1 * 2)
==> 1515
print(type(input1))
==> <class 'str'>




input2 = input('type number: ')
# '15'を入力してEnter
==> type number: 15 (ターミナルでのinputに対する出力と入力)
# intクラスのコンストラクトを使用してint型に変換して計算
converted_input2 = int(input2)
print(converted_input2 * 2)
==> 30
print(type(converted_input2))
==> <class 'int'>




# 変換処理時のエラーハンドリング:変換失敗例
try:
    input3 = input('type number: ')
    # 'Java'と入力してEnter
    ==> type number: Java (ターミナルでのinputに対する出力と入力)
    # 入力値が数値に変換できないのでValueErrorが発生
    converted_input3 = int(input3)
except ValueError:
    print('NOT number:', input3)
else:
    print(converted_input3)
==> NOT number: Java

関連情報:例外処理(try,except,else,finally)の使用方法

その他

strクラスのメソッドであるsplit を使用すると、フォーマットされた形式(csvなど)の入力を受け付けた後にlistに変換することが可能です。

また、 while構文:=(セイウチ演算子、原文は'the walrus operator')を使用することで、連続した入力を受け付けることもできます。

input1 = input('type comma separated values: ')
# 'apple,banana,kiwi'と入力してEnter
==> type comma separated values: apple,banana,kiwi (ターミナルでのinputに対する出力と入力)
print(input1)
==> apple,banana,kiwi
# strクラスのsplitメソッドを使用してlistを作成
list1 = input1.split(',')
print(list1)
==> ['apple', 'banana', 'kiwi']




# 1回目に'py'、2回目に'js'、3回目に'ts'、4回目は何も入力せずにEnter
while input2 := input('type anything: '):
    print(input2)
==> type anything: py (ターミナルでのinputに対する出力と入力)
==> py
==> type anything: js (ターミナルでのinputに対する出力と入力)
==> js
==> type anything: ts (ターミナルでのinputに対する出力と入力)
==> ts
==> type anything: (ターミナルでのinputに対する出力)

1次情報

input - Pythonドキュメント

代入式 - Pythonドキュメント