우리가 살아가는 도시

Python #8_기초부터 차근차근 본문

내가 알아가는 도시/Python

Python #8_기초부터 차근차근

세화주 2016. 7. 7. 14:53

'''포맷 코드
%s : 문자열(string)
%d : 정수형(Integer)
%f : 실수형(float)
%c : 문자(character)
%o : 8진수
%x : 16진수
%% : 리터럴%
'''

 

#print("완치될 확률은 %d% 입니다." %70)

==============================================================================

Example 1)

print("완치될 확률은 %d%% 입니다." %70)

 

[출력결과]

완치될 확률은 70% 입니다.

==============================================================================

'''[포멧코드의 활용 예]
 
  소수점 표현하기'''
 
#5는 소수점 뒷의 개수
==============================================================================

Example 2)

print("%0.5f" %2.454545)

 

[출력결과]

2.45454

==============================================================================

#정열과 공백처리
#전체를 10자리로 잡고 hello를 배치, 10은 양수이기 때문에 오른쪽 정렬이므로 끝에서부터 배치 됨

 

#( )( )( )( )( )(h)(e)(l)(l)(o)

==============================================================================

Example 3)
print("%10s" %"hello")

 

[출력결과]

     hello

==============================================================================

 

#(h)(e)(l)(l)(o)( )( )( )( )( )

==============================================================================

Example 4)
print("%-10s" %"hello")

 

[출력결과]

hello    

==============================================================================

Example 5)
print("%-15sPython!!" %"hello")

 

[출력결과]

hello          Python!!

==============================================================================

Example 6)
print("%-6sPython!!" %"hello")

 

[출력결과]

hello Python!!

==============================================================================