마이크로파이썬에서 str 자료형의 f-string, format( ), %formatting
프롬프트 영역으로 마우스를 옮기고 Copy 버튼을 누르면 클립보드에 프롬프트의 내용이 복사됩니다.마이크로파이썬에서 str 자료형의 변수를 출력할 때 사용하는 f-string, format( ), %formatting에 대한 프로그램을 만들고, 코드를 단계별로 설명해 줘.
AI가 만든 결과
아래 예제 코드는 마이크로파이썬에서 문자열 변수를 다양한 방식(% 포매팅, str.format(), f-string)으로 출력하는 방법을 보여줍니다. 다만 MicroPython의 버전에 따라 f-string 문법이 지원되지 않을 수도 있으니 사용 환경(버전)을 확인하시기 바랍니다.
1. 전체 예제 코드
def formatting_examples():
name = "John"
age = 30
temperature = 36.5678
# 1) C 스타일 % 포매팅
print("1) %% 포매팅 예시:")
print("Hello, my name is %s and I'm %d years old." % (name, age))
print("Current temperature is %.2f°C." % temperature) # 소수점 둘째 자리까지 출력
print()
# 2) str.format() 메서드
print("2) str.format() 예시:")
print("Hello, my name is {} and I'm {} years old.".format(name, age))
print("Current temperature is {:.2f}°C.".format(temperature))
print()
# 3) f-string (MicroPython 버전에 따라 미지원 가능)
print("3) f-string 예시:")
print(f"Hello, my name is {name} and I'm {age} years old.")
print(f"Current temperature is {temperature:.2f}°C.")
# 함수 호출
formatting_examples()
2. 코드 단계별 설명
2-1. 함수 정의 및 변수 초기화
def formatting_examples():
name = "John"
age = 30
temperature = 36.5678
formatting_examples()라는 함수를 정의합니다.- 예시로 사용할 세 변수를 선언:
name: 문자열 “John”age: 정수 30temperature: 부동소수점(실수) 36.5678
이 변수들을 각각 다른 포매팅 방법으로 출력해볼 것입니다.
2-2. C 스타일 % 포매팅
# 1) C 스타일 % 포매팅
print("1) %% 포매팅 예시:")
print("Hello, my name is %s and I'm %d years old." % (name, age))
print("Current temperature is %.2f°C." % temperature) # 소수점 둘째 자리까지 출력
print()
print("1) %% 포매팅 예시:")- 문자열에
%%를 쓰면 실제로는%한 개만 출력됩니다(이스케이프).
- 문자열에
- C 스타일 포매팅 문법
"%s": 문자열 삽입"%d": 정수 삽입"%.2f": 소수점 둘째 자리까지 표현하는 실수 삽입
- 예시 출력:
1) % 포매팅 예시: Hello, my name is John and I'm 30 years old. Current temperature is 36.57°C.
2-3. str.format() 메서드
# 2) str.format() 예시:
print("2) str.format() 예시:")
print("Hello, my name is {} and I'm {} years old.".format(name, age))
print("Current temperature is {:.2f}°C.".format(temperature))
print()
str.format()을 사용하면 중괄호{}가 있는 위치에.format()의 인자를 순서대로 대입합니다.{:.2f}처럼 포맷 지정자를 사용하여 소수점 아래 둘째 자리까지 표현 가능합니다.- 예시 출력:
2) str.format() 예시: Hello, my name is John and I'm 30 years old. Current temperature is 36.57°C.
2-4. f-string
# 3) f-string 예시:
print("3) f-string 예시:")
print(f"Hello, my name is {name} and I'm {age} years old.")
print(f"Current temperature is {temperature:.2f}°C.")
- f-string은 파이썬 3.6 이상에서 지원되는 기능이며, 문자열 앞에
f를 붙이고 중괄호{}안에 변수를 바로 넣어 표현합니다. "{temperature:.2f}"와 같은 방식으로 포매팅 옵션(.2f)도 사용 가능합니다.- 마이크로파이썬에서는 버전에 따라 f-string 문법을 지원하지 않을 수 있으니 주의해야 합니다.
- 예시 출력:
3) f-string 예시: Hello, my name is John and I'm 30 years old. Current temperature is 36.57°C.
3. 함수 실행 결과
전체 코드를 실행하면 콘솔(또는 REPL)에서 다음과 같은 출력이 예상됩니다(소수점 아래 자릿수는 지정한 대로 둘째 자리까지 반올림됨):
1) % 포매팅 예시: Hello, my name is John and I'm 30 years old. Current temperature is 36.57°C. 2) str.format() 예시: Hello, my name is John and I'm 30 years old. Current temperature is 36.57°C. 3) f-string 예시: Hello, my name is John and I'm 30 years old. Current temperature is 36.57°C.
4. 요약
% 포매팅
- 전통적인 C 스타일 포매팅 방법
- 문자열(
%s), 정수(%d), 실수(%f) 등에 대해 각각 서식을 지정
str.format()- 중괄호
{}위치에.format()으로 전달되는 인자를 순서대로 매핑 - Python 2.6+부터 지원되는 방식이며, MicroPython에서도 비교적 광범위하게 사용 가능
- 중괄호
f-string
- 가장 최신 문법이지만, MicroPython 버전에 따라 지원되지 않을 수도 있음
- 가독성이 높고, 중괄호 안에서 간단한 표현식도 가능
주의사항
- MicroPython은 일반 파이썬보다 기능이 제한될 수 있습니다.
- 특정 라이브러리나 최신 파이썬 기능(예: f-string)이 지원되지 않을 수 있으므로, 버전 정보를 확인해야 합니다.
각 상황에 따라 가장 호환성과 편의성이 높은 방법을 선택해 사용할 수 있습니다.
- 오래된 버전이나 호환성 우선 시:
% 포매팅이나str.format() - 최신 버전에서 간단하고 직관적 표현: f-string