LoginSignup
11
12

More than 5 years have passed since last update.

【質問】pythonで%を使うとどうなるの?

Last updated at Posted at 2015-07-27

pythonのreturn文やprint文で%がつかわれているのを度々見ます。
しかし、%の使い方はネットで調べてもピンとくるものがありません。

例えば下のコードは、二分グラフを作るコードの一部を抜き出したものですが、return文内で%が使われています。

class Vertex(object):
    """A Vertex is a node in  a graph"""
    def __init__(self, label = ''):
        self.label = label
    def __repr__(self):
        return 'Vertex(%s)' % repr(self.label)
    __str__ = __repr__

質問

  1. この%はどのような意味があるのでしょうか。
  2. 同時に、__str__ = __repr__ の意味もよくわからないのでお聞かせねがいたいです。
11
12
6

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
11
12