인프런 커뮤니티 질문&답변
이해가 잘 안돼요
작성
·
177
0
python('%10s' % ('nice'))를 하면 왼쪽부터 공백이 생겨서 ______nice가 돼서 문자가 오른쪽에 나오잖아요. 근데 python('%.5s' % ('pythonstudy'))에서는 5s 숫자가 양수인데 문자가 출력되는 방식이 음수 숫자를 이용해서 출력한것 처럼 왼쪽부터 문자가 출력돼서 pytho 이 나오는데 이 부분이 이해가 잘 안됩니다.
5s는 양수라 10s 처럼 문자가 오른쪽에 나와야되는데 왜 왼쪽부터 먼저 나오는지 궁금해요.
답변 1
1
좋은사람
지식공유자
안녕하세요.
패딩 유무의 차이입니다. 아래 예제를 확인해 주세요.
Truncating long strings
Inverse to padding it is also possible to truncate overly long values to a specific number of characters.
The number behind a . in the format specifies the precision of the output. For strings that means that the output is truncated to the specified length. In our example this would be 5 characters.
Old
'%.5s' % ('xylophone',)
New
'{:.5}'.format('xylophone')
Output
xylop
Combining truncating and padding
It is also possible to combine truncating and padding:
Old
'%-10.5s' % ('xylophone',)
New
'{:10.5}'.format('xylophone')
Output
xylop





