개발은 처음이라 개발새발

[Python]파이썬 프로그래밍 기초 - Print 옵션 1 본문

파이썬

[Python]파이썬 프로그래밍 기초 - Print 옵션 1

leon_choi 2024. 10. 20. 14:07
반응형

https://python-course.eu/python-tutorial/formatted-output.php

 

22. Formatted Output | Python Tutorial | python-course.eu

22. Formatted Output By Bernd Klein. Last modified: 08 Nov 2023. Many Ways for a Nicer Output In this chapter of our Python tutorial we will have a closer look at the various ways of creating nicer output in Python. We present all the different ways, but w

python-course.eu

 

print 옵션

 

1. separator :  sep = ''으로 활용되며 분리 옵션이다. 예를 들어서 웹에서 전화번호를 긁어왔을 때 010, 7777, 7777 처럼 전화 번호가 모두 분리 되어 있을 때 sep='-' 옵션을 추가하면 010-7777-7777로 만들어줘 우리가 아는 전화번호 형식으로 출력할 수 있다.

 

2. end :  end옵션을 사용하게 되면 뒤의 출력값이 있을 경우, 줄 바꿈을 하지 않고 이어서 출력한다. 

 

3. file : 출력한 구문을 하드디스크에 있는 특정 파일에 적을 수 있는 옵션이다. 

 

4. format(%d, %s, %f) : %s : 문자열 , %d : 정수 , %f : 실수 로 특정 서식에 따라 문자를 출력할 수 있다. 예를 들어서 pritn('%s

%s' % ('one','two'))의 구문을 출력하게 되면 '%s'(문자열)이라는  특정 서식을 명확하게 지정해줬기 때문에 결과물은 'one' 'two'가 출력된다.  같은 결과를 도출하지만 좀 더 간단한 구문은 print('{} {}'.format( 'one','two' ))이다. print('{} {}'.format( 'one','two' ))은 특정 서식을 지정해준 것이 아니기 때문에 print('{} {}'.format( 'one',2))를 출력하면 'one' 2가 출력된다.  print('{} {}'.format( 'one','two' ))에서는 {}안에 숫자를 넣어 순서를 지정해줄 수 있다. 예를 들어  print('{1} {0}'.format( 'one','two' ))를 출력하면 'two' 'one'을 출력할 수 있다.

# 기본 출력
print('Python Start!') 
print("Python Start!") 
print("""Python Start!""")
print('''Python Start!''')

print()

# separator 옵션 사용
print('P', 'Y', 'T', 'H','O','N', sep='')
print('010', '7777', '7777', sep='-')
print('python', 'google.com', sep='@')

print()

# end 옵션 사용
print('Welcome To', end=' ')
print('IT News', end=' ')
print('Web Site')

print()

# file 옵션 사용
import sys

print('Learn Python', file=sys.stdout)

print()

# format 사용(d, s, f)
print('%s %s' % ('one', 'two'))
print('{} {}'.format('one', 'two'))
print('{1} {0}'.format('one', 'two'))
반응형