Inflearn brand logo image

Inflearn Community Q&A

ilman124591031's profile image
ilman124591031

asked

Free Python Tutorial (Basic) - Become a Developer in 6 Hours

Various output formats

print"{0:,}".format(100000000) 3자리

Written on

·

223

2

print"{0:,}".format(100000000) 

출력된 건 100,000,00,000... 대충이정도

출력하면 3자리 마다  , 찍히는 자리숫자 설정 어떻게 하나요 

3자리 외 2자리나 1자리 5자리 원할수도있는데  어떤걸로 설정하나요 

python

Answer 1

0

nadocoding님의 프로필 이미지
nadocoding
Instructor

일반적으로 수를 적을 때 1000 단위로 콤마를 찍기 때문에 강의에서 설명한 대로

print("{0:,}".format(100000000000)) # 3자리마다 출력

이렇게 하면 간단히 3자리마다 콤마를 찍어줍니다.

원하시는대로 자릿수를 다르게 지정하시려면 아래와 같은 식으로 처리할 수 있습니다.

from itertools import zip_longest
num = "100000000000"
sep = ","
places = 5 # 콤마를 넣을 자릿수 설정
args = [iter(num[::-1])] * places
print(sep.join("".join(x) for x in zip_longest(*args, fillvalue=""))[::-1])

이 코드는 우리 수업의 범위 및 난이도를 벗어나므로 자세한 설명은 생략하겠습니다.

감사합니다.

ilman124591031's profile image
ilman124591031

asked

Ask a question