This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 문자열 포맷팅 관련 | |
# 변수 모음 | |
name = "hi" | |
color = "빨강" | |
age = 30 | |
avg = 70 | |
date = 30 | |
# str(): 변수를 문자열로 인식 | |
print("오프라인 스터디 모임 날짜는 매월" + str(date) + "일로 선정되었습니다.") | |
# format(변수, '형태'): 출력서식에서 쓰임 | |
print("이름:", format(name, "s"), ", 평균: ", format(avg, "^10")) | |
# "s":문자 , "d":정수, "o":8진수, "^n": 가운데 n자릿수로 정렬(기본우측정렬) | |
# "10.4f": 10자리숫자의. 소수점4자리까지 f실수 | |
# f스트링 포맷팅: 제일 간단히 변수 활용 | |
print(f"나는 {age}살이며, {color}색을 좋아해요.") | |
# %포맷팅 | |
print("나는 %d살입니다." %20) | |
# %d는 숫자/ s는 문자열/ c는 char | |
# %010d -> 앞의 0은: 10자릿수 d정수를 앞부분부터 0으로 채움 | |
print("나는 %s색과 %s색을 좋아해요." %("파란", "빨강")) | |
# %()포맷팅은 맵핑 개수 일치해야 | |
# {} .format()포맷팅은 맵핑 개수는 상관x | |
print("나는 {0}색과 {1}색을 좋아해요." .format("파란", "빨강", "주황")) | |
print("나는 {age}살이며, {color}색을 좋아해요." .format(age = 20, color= "빨강")) | |
print("{0:>10}".format(500)) | |
# {0:>10} : 오른쪽 정렬을 하되, 총 10자리 공간을 확보 | |
print("{0:_<10}".format(100)) | |
# 왼쪽 정렬하고, 빈칸을 _로 채움 | |
print("{0:,}".format(1000000000)) | |
# 3자리 마다 콤마를 찍어주기 | |
print("{0:+,}".format(2000000000)) | |
# 양수로 콤마 | |
print("{0:^<+30,}".format(30000000000)) | |
print("{0:f}".format(5/3)) | |
# 소수점 출력 | |
print("{0:.2f}".format(5/3)) | |
# 소수점 3째 자리에서 반올림(2째자리까지 출력) | |
반응형
'Programming > 파이썬(python) - 라이브러리' 카테고리의 다른 글
파이썬(python)/ json/ api (0) | 2023.05.31 |
---|---|
파이썬(python)/ 기본 예제 (0) | 2023.05.31 |
파이썬(python)/ 파일 입출력 (0) | 2023.05.31 |
파이썬(python)/ 클래스 (0) | 2023.05.31 |
파이썬(python)/ 가변인자 (0) | 2023.05.31 |