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?

More than 3 years have passed since last update.

Python 備忘録<基本文法1>

Posted at

目次

  • 基本的な文法
  • format関数
  • 文字列
  • 辞書型

基本的な文法

format関数

# format関数
# コンソール空の入力受ける
name = input('名前を教えてください')
age = 18

# {}のところに変数nameの名前が入る
# 複数入れたい場合は、その分だけ{}を使用し、format関数の中にその分だけ引数を入れること
print('私の名前は{}です、年齢は{}です'.format(name, age))

文字列

fruit = 'apple'
print(fruit)
# コンソール上で<class 'str'>と出力することを確認
print(type(fruit))

# 同じ文字列を複数回出力することができる
print((fruit + " ") * 5)

# 文字列の場合、[]を使用すると文字列の中一文字を出力することができる
print(fruit[2])

msg = "ABCDEFGHIJKA"

print(msg.count("A"))

# 該当の文字で始まるかどうか調べることができる
# trueが返ってくる
print(msg.startswith('ABC'))
# falseが返ってくる
print(msg.startswith('JK'))

msg = ' ABCD '
print(msg)
# 両橋を削除する
print(msg.strip())
# 右端を削除する
print(msg.rstrip())
# 左端を削除する
print(msg.lstrip())

辞書型

# keyに対しての値を宣言する
cart = {'morning': "bread", 'after_noon': 'nudole', 'diner': "psuta"}

# 値を取り出したい時は、keyを選択する
print(cart['morning'])

# 存在しないkeyを選択した時はエラーが返る
# print(cart['hello'])

# keyの値を全て取得する
print(cart.keys())
# 値の一覧を全て取得する
print(cart.values())
# key+値を全て取得する
print(cart.items())

tmp_cart = {'snack': 'candy'}

cart.update(tmp_cart)
# 上のtmp_cartの内容が追加された物が出力されること
print(cart.items())
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?