🤍 전 강의 25% 할인 중 🤍

2024년 상반기를 돌아보고 하반기에도 함께 성장해요!
인프런이 준비한 25% 할인 받으러 가기 >>

  • 카테고리

    질문 & 답변
  • 세부 분야

    프로그래밍 언어

  • 해결 여부

    미해결

이해가 잘 안돼요

20.09.07 02:11 작성 조회수 125

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     
채널톡 아이콘