본문 바로가기

python

출력서식

파이썬 출력서식 참고 - %, format()

PyFormat: Using % and .format() for great good!

 

 

출력서식

%s 문자

%d 정수

%f 부동소수

print('****' * 10)
# 출력 서식
print(format(123.45678, '10.3f'))
print(format(123.45678, '10.3'))
print(format(123, '10d'))
print ('{0:.3f}'.format(1.0/3))
print ('{0:_^11}'.format('hello'))
print ('{name} wrote {book}'.format(name='Swaroop', book='A Byte of Python'))
print('이름:{0}, 가격:{1}'.format('마우스', 5000, '마우스', 5000))
print('이름:{}, 가격:{}'.format('마우스', 5000, '마우스', 5000))
print('이름:{1}, 가격:{0}'.format('마우스', 5000, '마우스', 5000))
print('이름:{0}, 가격:{1} 이름:{0}, 가격:{1}, 가격:{1}'.format('마우스', 5000))

print('나는 나이가 %d 이다.'%23)
print('나는 나이가 %s 이다.'%'스물셋')
print('나는 나이가 %d 이고 이름은 %s이다.'%(23, '홍길동'))
print('나는 나이가 %s 이고 이름은 %s이다.'%(23, '홍길동'))
print('나는 키가 %f이고, 에너지가 %d%%.'%(177.7, 100))

****************************************
   123.457
  1.23e+02
       123
0.333
___hello___
Swaroop wrote A Byte of Python
이름:마우스, 가격:5000
이름:마우스, 가격:5000
이름:5000, 가격:마우스
이름:마우스, 가격:5000 이름:마우스, 가격:5000, 가격:5000
나는 나이가 23 이다.
나는 나이가 스물셋 이다.
나는 나이가 23 이고 이름은 홍길동이다.
나는 나이가 23 이고 이름은 홍길동이다.
나는 키가 177.700000이고, 에너지가 100%.

 

 

/t(tab), r(raw string)

print('~~~' * 10)
print('aa\tbb')
print(r'aa\tbb') # raw string을 선행하면 이스케이프 기능 해제
print("c:\aa\bbc\nbc.txt")
print(r"c:\aa\bbc\nbc.txt")

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
aa bb
aa\tbb
c:abc
bc.txt
c:\aa\bbc\nbc.txt

 

/t - tab 표시

/n - 개행기호

r(raw string) - 이스케이프 기능 해제

 

 

end=',' - 개행기호 무시

print('aa', end=',')
print('bb')

aa,bb

 

'python' 카테고리의 다른 글

python 정규표현식  (1) 2022.10.04
tuple, set, dict  (1) 2022.10.04
문자열 자료형, 리스트 자료형, deepcopy, stack queue  (0) 2022.10.04
연산자  (0) 2022.09.30
python과 자료형 기초  (1) 2022.09.30