상세 컨텐츠

본문 제목

3. print 함수

파이썬 기초

by js-delog77 2023. 5. 6. 15:03

본문

출력

파이썬에서 출력하기 위해서는 print문이 필요

- ' '  또는 " " 로 묶어서 문자를 출력하지만 ' ' 도 포함시켜서 사용하고 싶으면 " '내용 '  "이런식으로 묶어서 출력

- """ """ (''' ''') 이렇게 세쌍씩 묶어서 출력해도 " " 이렇게 출력과 같음.

- print문 자체에 줄 바꿈 기능이 포함 

print('Hello Python!')
print("Hello Python!")
print("""Hello Python!""")
print('''Hello Python!''')

# 전부 Hello Python!으로 출력

 

Separator (sep= ) 옵션 

print문의 ,(콤마) 사이를 연결해줄 때 사용 

print('T', 'E','S','T', sep='')  
print('2023','02','24',sep='-') 
print('niceMan','google.com', sep='@')

# 1. TEST 출력
# 2. 2023-02-24 출력
# 3. niceMan@google.com 출력

end(end= ) 옵션 

- print문의 줄바꿈 없애줌

-다음 print문의 문장과 연결(end= '내용' 입력 시 해당 print문의 뒤에 내용이 붙음

print('Welcome To', end='')
print(' the Black Paradice',end='')
print(' Piano notes')

# Welcome To the Black Paradice Piano notes 출력

format 옵션 

{중괄호}를 활용하여 사용- format으로 값을 초기화 

#format 사용 [대괄호]{중괄호}(소괄호)

#변수를 활용하여 출력 
print('{a} and {b}'.format(a='hi',b='hello'))         

# hi and hello 출력
# .format(변수에 직접 값 입력) 숫자 입력도 가능
print('{c} and {d}'.format(c=1,d=2))

# 1 and 2 출력

 

# format에 차례대로 값 입력
print('{} and {}'.format('you','me'))

# you and me 출력
# 인덱스처럼 사용 가능
print('{0} and {1} and {0}'.format('you','me'))

# you and me and you
%s: 문자,
%d: 정수
%f:실수
 
\n: 행(줄) 바꿈
\t: 탭(뛰어쓰기- 몇글자 간격)

 

'파이썬 기초' 카테고리의 다른 글

4-2. 가상환경 구성하기2  (0) 2023.05.06
4. 가상환경 구성하기  (0) 2023.05.06
인코딩  (0) 2023.05.06
2. vs code에 python 적용  (0) 2023.05.05
1. 파이썬 개발환경 설정(+ vscode 다운 받아놓기)  (0) 2023.05.05

관련글 더보기