[[채우기]자리맞춤][기호][#][0][너비][,][.정밀도][타입]

 


1. 너비

>>> print('**{1:10}**{0:2}**'.format(222, 111))
	**       111**222** # 111 왼쪽에 7만큼 공간이 생김(우측정렬)
>>> 
>>> print('{0:03}'.format(1)) # 0 채우기
	003

- 콜론(:) 좌측의 색인숫자와 헤깔리지 않도록 주의

 


2. 텍스트 조정: '채우기'와 '자리맞춤' 문자

>>> print('{:>7}'.format('Tom')) 	# '    Tom'
>>> print('{:@>7}'.format('Tom')) 	# '@@@@Tom'
>>> print('{:*<7}'.format('Tom')) 	# 'Tom****'
>>> print('{:*^7}'.format('Tom')) 	# '**Tom**'
>>> 
>>> print('{:=8}'.format('-1250')) 	# '-    1250'
>>> print('{:0=8}'.format('-1250')) # '- 0001250'

 


3. 기호 문자

>>> print('results>{: }, {:+}, {:-}'.format(25,25,25))
	results> 25,+25,25


4. 천 단위 위치구분자(,)

>>> print('The amount is {:,}'.format(13000))
	The amount is 13,000

 

5. 정밀도 제어

>>> # .m
>>> pi = 3.14159265
>>> print('{0:.1} {0:.2} {0:.4}'.format(pi)) 
	3e+00 3.1 3.142
	- .뒤 숫자만큼 숫자노출(정수+실수부) 
	- 자동 반올림
>>> 
>>> # n.m
>>> fss = '{:10,.3f}\n{:10.3f}'
>>> print(fss.format(22333.1, 1000.007))
>>> 22,333.100
	 1,000.007
	- ','나 '.'도 자릿수(10)에 포함된다.
>>> 
>>> # 문자열
>>> fss = '{:*<6.6}' # 왼쪽정렬(<) 빈공간채우는문자(*) 공간(6) 사용할 자리수(6)
>>> print(fss.format('Tom'))
>>> print(fss.format('Rodney'))
>>> print(fss.format('Hannibal'))
	Tom***
	Rodney
	Hannib

 

6. 타입 지시자

>>> # '#' 접두사 - 진수앞에 형식자를 붙여줌(0b, 0o, 0x)
>>> print('{0:X}\n{0:#X}'.format(int('FF',16)))
	FF
	0XFF

 

 

+ 변수-길이

>>> # 길이지정 필드를 변수로 대입 가능
>>> a, b = 10, 4
>>> 'Here is a num : {:{}.{}}'.format(1.2345, a, b)
	'Here is a num :      1.234'
>>> 
>>> '{0:{1}}{2:{3}}!'.format('Hi', 3, 'there', 7)
	'Hi there  !'

 

'Python > 공통이론' 카테고리의 다른 글

Python - 고급 정규표현식  (0) 2023.05.05
Python - 정규표현식  (0) 2023.04.25
Python - repr  (0) 2023.04.17
Python - format 메서드  (0) 2023.04.14
Python - format 함수  (0) 2023.04.14

+ Recent posts