본문 바로가기
PYTHON

format() 함수 예제 코딩

by devorldist 2022. 8. 28.
728x90
반응형
SMALL
# format() 함수로 숫자를 문자열로 변환하기
format_a = "{}만원".format(8000)
format_b = "How many hours should I study? The answer is {} hours".format(1000)
format_c = "{} {} {}".format(1000, 2000, 3000)
format_d = "{} {} {}".format(1, "스트링", False)

 

# 출력하기
print(format_a)
print(format_b)
print(format_c)
print(format_d)
 
##정수를 특정 칸에 출력하기 예제
# 정수
output_a = "{:d}".format(52)

# 특정 칸에 출력하기
output_b = "{:5d}".format(52)
output_c = "{:10d}".format(52)

# 빈 칸을 0으로 채우기
output_d = "{:05d}".format(52)
output_e = "{:05d}".format(-52)

print("#결과 출력하기")
print(output_a)
print(output_b)
print(output_c)
print(output_d)
print(output_e)
 
## 기호 붙여 출력하기
# 기호와 함께 출력하기
output_f = "{:+d}".format(52)
output_g = "{:+d}".format(-52)
output_h = "{: d}".format(52)
output_i = "{: d}".format(-52)

print("#기호와 함께 출력하기")
print(output_f)
print(output_g)
print(output_h)
print(output_i)
 
## 조합해 보기
output_h = "{:+5d}".format(52)
output_i = "{:+5d}".format(-52)
output_j = "{:=+5d}".format(52)
output_k = "{:=+5d}".format(-52)
output_l = "{:+05d}".format(52)
output_m = "{:+05d}".format(-52)

print("# 조합하기")
print(output_h)
print(output_i)
print(output_j)
print(output_k)
print(output_l)
print(output_m)
 
 
 
 
 
 
 
 출처 :  혼자 공부하는 파이썬
728x90
반응형
LIST