[Python]print() 기초
구문 : print()
정의 :
def print(self, *args, sep=' ', end='\n', file=None): # known special case of print """ print(value, ..., sep=' ', end='\n', file=sys.stdout, flush=False) Prints the values to a stream, or to sys.stdout by default. Optional keyword arguments: file: a file-like object (stream); defaults to the current sys.stdout. sep: string inserted between values, default a space. end: string appended after the last value, default a newline. flush: whether to forcibly flush the stream. """ pass
예제 :
""" 참고 : Escape 코드 \n : 개행 \t : 탭 \\ : \ \' : ' \" : " \000 : 널 문자 """ print('hello python') print("hello python") print('''hello python''') print("""hello python""") print() # sep 옵션, 문자열의 중간에 삽입하는 값, 기본값은 공백 print("p", "y", "t", "h", "o", "n", sep="") print('010', '1234', '4567', sep="-") print('python', 'jigi.net', sep="@") print() # end 옵션, 문단의 마지막에 넣는 문자열, 기본값은 줄바꿈 print('Welcome to', end=' ') print('my world', end=' ') print(', my home') print() # file 옵션, 지정한 문자열을 파일로 저장, 기본값은 콘솔출력(sys.stdout) import sys print('Learn python', file=sys.stdout) # format 사용(d : 정수, s : 문자열, f : 실수) print('%s %s' % ('one', 'two')) print('{} {}'.format('one', 'two')) print('{} {}'.format('one', 2)) print('{1} {0}'.format('one', 'two')) # 인덱스를 지정할 수 있다. => two one print() # %s print('%10s' % ('nice')) # 오른쪽 정렬(10자리) print('{:>10}'.format('nice')) # 오른쪽 정렬(10자리) print('%-10s' % ('nice')) # 왼쪽 정렬(10자리) print('{:10}'.format('nice')) # 왼쪽 정렬(10자리) print('{:_>10}'.format('nice')) # 오른쪽 정렬(10자리, _ 채움) print('{:_^10}'.format('nice')) # 중앙정렬(10자리, _ 채움) print('%.5s' % ('nice')) # 5자리 자름 print('%.5s' % ('python study')) # 5자리 자름 print('{:10.5}'.format('python study')) # 공간은 10자리를 확보하나, 5글자만 출력 print('{:_>10.5}'.format('python study')) # 오른쪽 정렬, 공간은 10자리를 확보하나, 5글자만 출력 # %d print('%d %d' % (1, 2)) print('{} {}'.format(1, 2)) print('%4d' % (42)) # 4자리 확보 print('{:4d}'.format(42)) # 4자리 확보, format 함수 사용시에는 정수표시 'd'를 붙여줘야 한다. # %f print('%f' % (12345678.123456789)) # 실수부는 6자리까지만 나온다. 7자리에서 반올림 print('%1.9f' % (12345678.123456789)) # 지정한 자리수만큼 실수가 표기 print('%1.20f' % (12345678.123456789)) # 지정한 자리수만큼 실수가 표기, 실제 입력한 실수자릿수 이후는 알수없는 숫자가 붙는다. 뭐지? print() print('{:f}'.format(1.234567890)) # 실수부는 6자리까지만 나온다. 7자리에서 반올림 > 1.234568 print('%06.2f' % (1.12345678)) # 총 자리수는 6자리, 정수부 6자리가 안되면 0으로 채움, 실수는 2자리만 표시 > 001.12 print('%6.2f' % (1.12345678)) # 총 자리수는 6자리, 정수부 6자리가 안되면 공백으로 채움, 실수는 2자리만 표시 > __1.12 print('{:06.2f}'.format(1.12345678)) # 총 자리수는 6자리, 정수부 6자리가 안되면 0으로 채움, 실수는 2자리만 표시 > 001.12 x = 50 y = 100 text = 123512312 n = 'jigi' ex1 = 'n = %s, s = %s, sum = %d' % (n, text, (x + y)) print(ex1) ex2 = 'n = {n}, s = {s}, sum = {sum}'.format(n=n, s=text, sum=x+y) print(ex2) ex3 = f'n = {n}, s = {text}, sum = {x+y}' # v3.6이후 버전에서 지원된 기능 print(ex3) print() # 구분기호 m = 10000000 print(f'm = {m:,}') # 1000 자리마다 콤마 찍음 # 정렬 # ^ : 가운데, < : 왼쪽, > : 오른쪽 정렬 t = 20 print(f't = {t:10}') # t = 20 print(f't = {t:>10}') # t = 20 print(f't = {t:_>10}') # t = ________20 print(f't = {t:^10}') # t = 20 print(f't = {t:-^10}') # t = ----20---- print(f't = {t:<10}') # t = 20 print(f't = {t:*<10}') # t = 20********
결과:
hello python hello python hello python hello python python 010-1234-4567 python@jigi.net Welcome to my world , my home Learn python one two one two one 2 two one nice nice nice nice ______nice ___nice___ nice pytho pytho _____pytho 1 2 1 2 42 42 12345678.123457 12345678.123456789 12345678.12345678918063640594 1.234568 001.12 1.12 001.12 n = jigi, s = 123512312, sum = 150 n = jigi, s = 123512312, sum = 150 n = jigi, s = 123512312, sum = 150 m = 10,000,000 t = 20 t = 20 t = ________20 t = 20 t = ----20---- t = 20 t = 20********
최신 댓글