[Python] end 옵션, file 옵션, format() 메소드, %기호

정만·2025년 3월 19일

데이터분석

목록 보기
4/61

파이썬 완전 기초, print 사용법 등을 학습한 내용이다.



# 기본 출력력
print('Python Start!')  #작은 따옴표를 가장 많이 사용
print("Python Start!")
print('''Python Start!''')
print("""Python Start!""")


print() #개행 역할을 함 


#seperator 옵션 사용하기 
print('P', 'Y', 'T', 'H', 'O', 'N', sep='')
print('2008ojm', 'naver.com', sep='@')
print('010', '2832', '1667', sep='-')


print()

#end 옵션 사용하기 > print 자체가 자동 줄바꿈을 하지만 end를 통해 한줄로 이울 수 있음

print('welcome to', end=' ')
print('IT News', end=' ')
print('Web site', end=' ')


print()

#file 옵션 사용하기 

import sys
print('Learn Python', file=sys.stdout)   #stdout은 콘솔 아웃을 의미, 해당 내용을 파일에 띄우기, 


print()

#format 사용하기 (d, s, f) > d = digit(정수), s = string, f = float 
print('%s %s' % ('one', 'two'))
print('{} {}'.format('one', 'two'))
print('{1} {0}'.format('one', 'two'))

print()


# %s 
print('%10s' % ('nice'))  # 10s는 10개의 자릿수를 의미하고 nice 앞에 공백으로 6자리 매꿈.
print('{:>10}'.format('nice'))

print('%-10s' % ('nice'))
print('{:10}'.format('nice'))

print('{:_>10}'.format('nice'))   # 공백이 언더바로 채워짐 
print('{:^10}'.format('nice'))   # 중앙정렬 


print('%.5s' % ('nice'))
print('%.5s' % ('niceverygood'))  #5의 크기만큼 절삭해서 나타냄
print('{:10.5}'.format('niceverygood'))



print()


# %d

print('%d %d' % (1,2))
print('{} {}'.format(1,2))

print('%4d' % (42))
print('{:4d}'.format (42))

print()


# %f

print('%f' % (3.12344541))
print('{:f}'.format(3.123456))

print('%06.2f' % (3.111232123))
print('{:06.2f}'.format(3.1231321))
profile
멋있는 어른이 되고싶은 정만이의 벨로그

0개의 댓글