Programming/python
python, print 시 문자열, 숫자, 자리수 조절하기
단창
2019. 8. 29. 15:50
strs =[ 'abc','abcd','ab']
nums = [12,1342, 0.241, 2141.5312]
for i in range(3):
print('%s %f'%(strs[i],nums[i]))
이렇게 출력하면
이렇게 삐뚤삐뚤하게 정렬된다.
문자열.ljust(n) : n개의 문자공간중 좌측정렬
%10.3f : 10의 출력폭(소수점포함) 과 3개의 소수점정밀도
strs =[ 'abc','abcd','ab']
nums = [12,1342, 0.241, 2141.5312]
for i in range(3):
print('%s %9.3f'%(strs[i].ljust(5),nums[i]))
이렇게 출력하면
문자열 숫자 모두 줄 맞춰 출력된다.
반응형