** 인프런 프로그래밍 시작하기 : 파이썬 입문강의를 참고하여 작성된 글입니다.
1. seperator
- 인수 사이의 결합
- ex) print( 1, 2, 3, sep = '-') 결과 : 1-2-3
1
2
3
4
|
#seperator 옵션 print('P', 'Y', 'T', 'H', 'O', 'N', sep='')
print('010', '7777', '1234', sep='-')
|
2. end
- 문장과 문장 연결 (다음에 올 문장을 따옴표 안의 것으로 연결)
- ex) print('Hello', end=' ') 결과 : Hello World
print('World')
1
2
3
4
|
#end 옵션 print('Welcome to', end=" ")
print('Python', end=' ')
print('World')
|
3. format
- 자료형 - %d는 정수, %s는 문자열, %f는 실수
- print('%d' %(3))과 print('{:d}'.fomat(3))는 같은 결과가 출력된다.
- 왼쪽 정렬, 오른쪽 정렬, 중앙 정렬, 원하는 자리수까지 출력 등 다양한 형태로 출력이 가능하다.
🥕밑의 실습 코드로 연습해보는 것을 추천합니다
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
|
#format 사용 print('%s %s' %('one', 'two'))
print('{} {}'.format('one', 'two'))
print('{1} {0}'.format('one', 'two'))
print()
# %s
#오른쪽 정렬
print('%10s' %('nice'))
print('{:>10}'.format('nice'))
#왼쪽 정렬
print('%-10s' %('nice'))
print('{:10}'.format('nice'))
#중앙 정렬
print('{:^10}'.format('nice'))
# 원하는 자리 수까지 출력
print('%.5s' %('nice'))
print('{:.5}'.format('GoodDay'))
print()
# %d
print('%d %d' %(1,2))
print('{} {}'.format(1,2))
print('%4d' %(12345))
print('{:4d}'.format(42)) #문자열과 다르게 자료형(d) 붙여줘야 함!
print()
# %f
print('%f' %(3.141592))
print('{:f}'.format(3.141592))
print('%06.2f' %(2.13412342))
print('{:06.2f}'.format(2.13412342))
|
'Programming study > Python' 카테고리의 다른 글
[Python] 숫자형, 문자형 (0) | 2021.07.15 |
---|---|
[Python] 변수 (0) | 2021.05.18 |
[Python] 좋은 코딩, 좋은 프로그램 작성하기 (0) | 2021.05.10 |
05 숫자와 문자열 (1) | 2020.08.13 |
04 주석 (0) | 2020.08.04 |