1
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のあんまり知らなそうな組み込みメソッド集 #1 [str.capitalize, int.bit_count, Exception.add_note]

1
Posted at

この記事を作った理由

Pythonで処理を書いたときに、「あ、これ標準の機能でできるじゃん!」
ということがあったりするので、最初からそれを使えるようにしたいな〜と思い
勉強とメモとして作りました!

str.capitalize

このメソッドは、文章の最初の文字を大文字にして、それ以外を小文字にします。

string = "hello, This is A Pen"
print(string.capitalize()) # Hello, this is a pen

int.bit_count

このメソッドでは、整数を二進法で見たときに、ビットが1である数を数えることができます。

num = 64
print(num.bit_count(), (num - 1).bit_count()) # 1 6

Exception.add_note

このメソッドを使うと例外に追加のメッセージを作ることができます。

error = Exception("エラーだよ")
error.add_note("このメッセージはadd_noteで追加されたものだよ")
raise error
Traceback (most recent call last):
  File "main.py", line 3, in <module>
    raise error
Exception: エラーだよ
このメッセージはadd_noteで追加されたものだよ

最後まで読んでいただきありがとうございます!
多分続きも作ります

1
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
1
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?