문자열 포맷팅
python 프로그래밍 - 문자열 포맷팅
파이썬에서는 여러 가지 방법으로 문자열을 포맷팅 할 수 있다.
[방법1] Fromat code
서식 문자 목록
%s - string
%c - character
%d - integer
%f - floating point
%o - octet
%x - hexadecimal
%% - Literal(verbatim) %
출력 양식 지정
%10s - 해당 문자열에 10칸 허용(길이가 10을 넘으면 잘린다)
%0.5f - 소수점 다섯 번재 자리까지 출력
% 문자가 escape character로 작용한다.
코드
print('\r progress: %d%%' % 100)
결과
progress: 100%
[방법2] format 함수
코드
print('date: {0}년 {1}월 {2}일'.format(2023, 3, 16))
print('date: {year}년 {month}월 {date}일'.format(year=2023, month=3, date=16))
결과
date: 2023년 3월 16일
[방법2] f-string
python 3.6부터 지원한다
코드
name = 'progress'
value = 100
print(f'{name}: {value}%')
결과
progress: 100%
콘솔의 커서 숨기기
방법1
cursor라는 python3 모듈을 설치하여 사용
$ pip install cursor
import cursor
cursor.hide()
cusrsor.show()
방법2
ANSI escape code를 사용
print('\033[?25l', end="") # hide cursor
print('\033[?25h', end="") # show it back
ANSI escape code
in-band signaling의 표준이다.
터미널에 대한 다음과 같은 control information을 표현한다.
- cursor location, color
- font styling
- other options
\033 + [ 로 시작하는 sequance of byte가 전송하고자 하는 텍스트(data)에 embed되면 터미널은 이 sequances를 출력할 문자가 아닌 명령으로 해석한다.
In-band signaling
Telecomunication에서 control information을 전송하고자 하는 데이터(voice, video, text)와 같은 대역폭이나 채널에 보내는 방식
Comments