
Print 사용법
# 참조 : https://www.python-course.eu/python3_formatted_output.php
다양한 print출력
separator사용
end사용
python Format사용
참고 : Escape 코드 \n : 개행 \t : 탭 \\ : 문자 \' : 문자 \" : 문자 \000 : 널 문자 ...
# 기본 출력
print('Python Start!') #자주사용 print("Python Start!") #자주사용 print("""Python Start!""") print('''Python Start!''') print() #그냥 프린트는 줄바꿈이 된다.
# separator 옵션 사용
print('P', 'Y', 'T', 'H','O','N') #P Y T H O N print('P', 'Y', 'T', 'H','O','N', sep='') #PYTHON #sep를 공백으로 하면 붙어서 나온다. print('010', '7777', '7777', sep='-') #010-7777-7777 print('python', 'google.com', sep='@') #python@google.com
# end 옵션 사용
print('Welcome To', end=' ') print('IT News', end=' ') print('Web Site') #welcome to IT News web site
print문은 자동으로 줄바꿈을 해주지만
end옵션을 넣으면 끝부분을 어떻게할건지 정할 수 있고 그 다음 print문으로 이어질수 있다.
# file 옵션 사용
import sys #import는 예약어이기 때문에 변수로 할당할 수 없다. print('Learn Python'), #file='test.txt') #print문의 내용을 외부에 지정한 파일인 test.txt에 쓰는 것이다. #(test.txt는 잘못된코드 일종의 예시를 보여주기 위해서 사용한것.) print('Learn Python', file=sys.stdout) #sys.stdout는 cmd+i를 눌러 나오는 콘솔아웃을 의미한다. #그래서 실행하면 그냥 똑같이 나오는 것이다.
# format 사용(d정수 :3 , s문자열: 'python', f실수: 3.144)
print('%s %s' % ('one', 'two')) #one two %는 일종의 다리 역할을 한다. print('{} {}'.format('one', 'two')) #one two print('{1} {0}'.format('one', 'two')) #two one
#프로그래밍은 첫 번째는 0부터 시작한다. 인덱스는 무조건 0부터 생각하자.
#암묵적으로 {} {}가 비어 있으면 {0}(1)순서대로 인것이다.
# %s (string)
print('%10s' % ('nice',)) # nice 10은 자릿수를 의미한다. print('{:>10}'.format('nice'))# nice print('%-10s' % ('nice',)) #nice -를 쓰면 왼쪽부터 채워서 10자리 print('{:10}'.format('nice'))#nice 위의 결과와 같음. print('{:<10}'.format('nice'))#nice 위의 결과와 같음. print('{:_<10}'.format('nice')) #______nice 공백을_로 바꿈 print('{:^10}'.format('nice')) # nice 중앙정렬 print('%.5s' % ('pythonstudy',)) #pytho 점(.)이 붙으면 5글자만 절삭을 한다. print('%5s' % ('pythonstudy',)) #pythonstudy 점이 없으면 내가 공간을 5개만 확보했더라도 그냥 다 들어온다. print('{:.5}'.format('nice'))#nice(공백없음) print('{:.5}'.format('pythonstudy'))#pytho print('{:10.5}'.format('pythonstudy'))#pytho (6칸 공백) #총 10글자의 자리를 확보하는 5글자민 나와라 라는 뜻. 다만, 공간은 10개를 가지고 있다.
# %d (digit)
print('%d %d' % (1, 2)) #1 2 print('{} {}'.format(1, 2)) #1 2 print('%4d' % (42)) # 42 (42,)라고 적어도 된다. print('%4d' % (4254354)) #4254354 4자리를 정해도 값이 그대로 출력된다. print('{:4d}'.format(42)) # 42 정수일 때는 4뒤에 d를 붙여줘야한다.
print('%4d' % (42)) # 42 (42,)라고 적어도 된다.
# %f (float)
print('%f' % (3.141592653589793,)) #3.141593 print('%1.8f' % (3.141592653589793,)) #3.14159265 정수부는 한 자리만 나오고 소수부는 8자리까지 나온다 print('{:f}'.format(3.141592653589793)) print('%06.2f' % (3.141592653589793,)) #003.14 print('{:06.2f}'.format(3.141592653589793))#003.14

print('%10.2f' % (12345678.923,)) #12345678.92
12345678.932는 폭이 12이고 10.2f는 10자리의 공백이다.
폭은 정수부분을 지칭하기도 하니 먼저 12345678을 쓴다.
그 다음 소수점(.)을 붙이고 소수점 첫자리인 9를 써서 10자리를 맞춘다.
12345678.9
하지만 2f가 붙어서 소수점이 2자리로 고정이기 때문에
12345678.92로 11자리가 되는 것이다.
'Python > Today I learned' 카테고리의 다른 글
TIL#14 데이터타입(자료형)-숫자형 (0) | 2021.05.14 |
---|---|
TIL#13 변수 (0) | 2021.05.13 |
TIL#11 생활코딩 Python 수업을 마치며 (0) | 2021.05.12 |
TIL#10 생활코딩 Python 보안,pypi,API (0) | 2021.05.12 |
TIL#09 생활코딩 Python 모듈,리팩토링 (0) | 2021.05.12 |