LoginSignup
0
1

More than 1 year has passed since last update.

python初学者の備忘録 print()関数について

Last updated at Posted at 2021-09-08

←目次へ

python,Qiita初心者なので、備忘録として記載していきます。 なにせ初心者なので、知識不足はご理解ください。 知識を深めながら追記していきたいと思います。

print()関数とは

基本

  • print(値)で「値」を出力できる
  • ()の中を「,」で区切る事で複数の値を出力できる
  • ()の中で演算も行うことができる
型毎の出力
# -- 数値型 --
>>> print(1)
1

# -- 浮動小数型 --
>>> print(0.100100)
0.1001

# -- ''で囲うと出力結果が数値型に見えるが文字列 --
>>> print('0.100100')
0.100100
print('0.100100' type('0.100100'))
0.100100 #<class 'str'>

# -- 文字列型 --
>>> print('Hello')
Hello

# -- 複数値 --
>>> print(1, 1)
1 1
>>> print('a', 'b', 'c')
a b c

# -- 演算 --
>>> print(1 + 2)
3

# -- 不等式 --
print(2 > 1)
True
print(2 < 1)
False
文字列の連結
>>> print('テストの点数は' + 100 + '点です')
# 文字と数字は結合出来ないのでエラー

>>> print('テストの点数は' + '100' + '点です')
または
>>> print('テストの点数は' + str(100) + '点です')
テストの点は100点です

print('おはよう' * 6)
おはようおはようおはようおはようおはようおはよう

>>> print('おはよう' * 6 + 'こんにちは')
おはようおはようおはようおはようおはようおはようこんにちは

>>> print('おはよう' * 6 - 'こんにちは')
# 文字列の引き算は出来ないためエラー

>>> print('こんにちは' + 'おはよう' * 6)
こんにちはおはようおはようおはようおはようおはようおはよう
# 文字列結合の場合、演算子の優先順位は関係なくなる

print()関数のオプション

sep:区切り文字を指定して文字を連結する
print_option.py
# -- オプションなし -- 
print('おはよう','こんにちは','こんばんは','またあした')
# -- 出力結果 --
おはようこんにちはこんばんはまたあした

# -- ,を指定する -- 
print('おはよう','こんにちは','こんばんは','またあした', sep=',')
# -- 出力結果 --
おはよう,こんにちは,こんばんは,またあした
end:末尾に文字を指定して連結する
print_option.py
# -- 末尾に改行コードを挿入 --
print('おはよう','こんにちは', sep=',', end='\n')
print('こんばんは','またあした', sep=',')
# -- 出力結果 --
おはよう,こんにちは
こんばんは,またあした
0
1
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
1