0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

python 出力(print)について

0
Posted at

はじめに

自分が今覚えているpythonの様々な出力方法を書いていきます。

目次

文字列を出力する

printに' '" "で囲った文字列を渡してあげると渡した文字列を返してくれる。

print string
print('hello')
# hello
print("lets enjoy programing")
# lets enjoy programing

' '" "がないと文字列として認識されない。
下の例はprintに渡したaiueoが文字列ではなく変数として認識されて、変数が定義されてないよとエラーが出ている。

pirnt string error
print(aiueo)
# NameError: name 'aiueo' is not defined.

数字を出力する

printにそのまま数字を入れると数字を出力できる。

print number
print(123)
# 123

文字が入るとエラーが出る。数字が先頭の場合とそうでない場合でエラーの種類が違う。
前者は構文エラーで変数の前に数字があったり、指数表記例:1e-3を正しく記述していない場合に出る。
後者は変数として認識されるため定義されていないとエラーが出ている。

print number error
print(123four)
print(1e-n)
# SyntaxError: invalid decimal literal

print(one234)
# NameError: name 'sdfs123' is not defined

変数の出力

定義された変数をprintに渡すと定義されたデータを出力する。

print variable
n = 123
print(n)
# 123

s = 'abc'
print(s)
# abc

文字列や数字だけではなくリスト:listタプル:tuple辞書:dictを渡して出力することができる。

print variable
l = [1, 2, 3]
print(l)
# [1, 2, 3]

t = (4, 5, 6)
print(t)
# (4, 5, 6)

d = {1: 'a', 2: 'b', 3: 'c'} 
print(d)
# {1: 'a', 2: 'b', 3: 'c'}

インデックスキーを指定して特定のデータを取得することができる。

print variable
l = ['a', 'b', 'c']
print(l[1])
# b

t = ('d', 'e', 'f')
print(t[0])
# d

d = {'first': 'a', 'second': 'b', 'third': 'c'}
print(d['third'])
# c

結合して出力する

文字列を+でつなぐと文字列同士を結合できる。

print connect data
print('first' + 'second')
# firstsecond

数値は結合できないので文字列に変形してから結合する。

print connect data
print(123 + 'first')
# TypeError: can only concatenate str (not "int") to str

n = 123
n = str(n)
print(n + 'first')
# 123first

結合はformatを使うことができる

use format
name = Taro
print('Hello {}'.format(name))
# Hello Taro

formatは複数の引数を渡すことができ、左から順にformatに渡した引数を{}に代入していく。また、変数に入った数値は文字列に変換しなくても良い。

use format
first = 1
second = 2
third = 3
print('一番目:{} 二番目:{} 三番目:{}'.format(first, second, third))
# 一番目:1 二番目:2 三番目:3

formatを使わずに上記の出力をする場合、下記のように見にくいコードになる。
使い分けができるよう、両方の書き方を覚えて読みやすいコードにしよう。

use format
first = '1'
second = '2'
third = '3'
print('一番目:' + first + ' 二番目:' + second + ' 三番目:' + third)
# 一番目:1 二番目:2 三番目:3

真偽(True, False)の出力

booleanの出力もすることができる。

print boolean
is_ok = True
print(is_ok)
# True

passwd = 'abc'
print(passwd == 'abc')
# True
print(passwd == 'ggg')
# False

Typeの出力

個人的によく使うもの。
pythonは動的に型を変換できるので、変数がどの型になっているのか把握するは大事。
変数名も工夫しよう!

print type
word = 'abc'
number = 123
my_list = ['first', 'second', 'third']
is_ok = True

print(type(word))
# <class 'str'>
print(type(number))
# <class 'int'>
print(type(my_list))
# <class 'list'>
print(type(is_ok))
# <class 'bool'>

終わりに

printだけでもたくさん覚えることがあって大変・・・

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?