2016-10-25 1 views
-1

다시 :) 파이썬이 문을 코드형식 파이썬() 함수 - 여러 중괄호의 사용 {{{}}}

col_width=[13,11] 
header=['First Name','Last Name'] 

format_specs = ["{{:{}}}".format(col_width[i]) for i in range(len(col_width))] 
lheader=[format_specs[i].format(self.__header[i]) for i in range(nb_columns)] 

의이 비트를 평가

내가 발견하는 방법? 우리가 세 가지를 사용하는 이유 (모든 반복에서 하나의 요소를 형식화 할 때)?

+2

왜 이것을 파이썬 2 및 파이썬 3 태그로 태그 지정합니까? 무엇 이니? 상관 없니? 부적절한 태그를 사용하지 마십시오. –

+0

[docs] (https://docs.python.org/3/library/string.html#formatstrings)를 읽어보십시오. – user2357112

+0

고마워요 :) –

답변

0

{{}} 일 때 파이썬은 {}의 대체를 건너 뛰고 string의 일부로 만듭니다. 마찬가지로, 당신의 표현으로 평가하고있다

>>> '{{}}'.format(3) # with two '{{}}' 
'{}' # nothing added to the string, instead made inner `{}` as the part of string 
>>> '{{{}}}'.format(3) # with three '{{{}}}' 
'{3}' # replaced third one with the number 

:

자세한 내용
>>> '{{:{}}}'.format(3) 
'{:3}' # during creation of "format_specs" 

는, 참조 : Format String Syntax 문서 다음은이를 설명하는 샘플 예제입니다.

+1

고맙습니다 :) –