Python 3 print 하나만 쓰고 여러 값을 여러 줄에 나타내기

Python 2014. 9. 30. 21:54
print(str(a.sum()) + '\n' + str(a.sub()) + '\n' + str(a.mul()) + '\n' + str(a.div())

a.sum()들을 문자로 바꾸기


print('{0}\n{1}\n{2}\n{3}'.format(a.sum(), a.sub(), a.mul(), a.div()))

 |  format(...)
 |      S.format(*args, **kwargs) -> str
 |     
 |      Return a formatted version of S, using substitutions from args and kwargs.
 |      The substitutions are identified by braces ('{' and '}').


print('%d\n%d\n%d\n%d' % (a.sum(), a.sub(), a.mul(), a.div()))

%d나 %s를 이용


print(a.sum(), a.sub(), a.mul(), a.div(), sep='\n')

sep:   string inserted between values, default a space.


print(a.sum(), '\n', a.sub(), '\n', a.mul(), '\n', a.div(), sep='')

기본적으로 sep=' '인데 ''으로 바꿔줌


class FourCal:
    def __init__(self, first, second):
        self.first = first
        self.second = second
    def sum(self):
        return self.first + self.second
    def sub(self):
        return self.first - self.second
    def mul(self):
        return self.first * self.second
    def div(self):
        return self.first / self.second

'Python' 카테고리의 다른 글

을미년 복면산  (0) 2015.04.27
comments powered by Disqus

설정

트랙백

댓글