2020. 6. 22. 18:42 Python/Python Script
Printer.py - string format 연습
반응형
proj
- Test
...
L Printer.py
from Test.Tester import Tester
class Printer(Tester):
def __init__(self):
super().__init__(self.PrintTest)
pass
def PrintTest(self):
"""
각종 string formatting들
:return:
"""
test_str = []
#일반 문자열
test_str.extend(
[
"This is basic print"
]
)
#% 연산자 활용방식, 단 args들의 타입을 정확히 명시해줘야함, 인자 많아질수록 코드 더러워짐
test_str.extend(
[
"This way using operator %s %i" % ("haha", 2)
]
)
# C#에서도 쓰는 formatting
# 인자 숫자에 맞춰 출력해주거나 아예 생략도 가능하다.
# 다만 이방식도 다소 장황해질 수 있다.
test_str.extend(
[
"This is basic formatting - {1}{0}".format("hehehe", 100),
'For simple {}-{}'.format("hoomba", "HOOMBA"),
]
)
#개인적으론 제일 좋은것 같다.
# f-string 이라고 하며, 3.6버전 이상부터 지원한다고 한다.
reply = 'yes captain'
f_str = f'Hmm interesting {reply}'
test_str.extend([
f_str,
])
for ele in test_str:
print(ele)
input_val = input("press any key")
self.FinTest()
pass
pass
python에서 지원하는 string format에 대한 연습 코드.
* list의 extend와 append에는 차이가 있으니 검색해볼것.
더보기
====결과
Hmm interesting yes captain
This is basic print
This way using operator haha 2
This is basic formatting - 100hehehe
For simple hoomba-HOOMBA
Hmm interesting yes captain
press any key
# test 결과 확인 후 임의의 입력 후 엔터누르면 종료됨.
반응형
'Python > Python Script' 카테고리의 다른 글
OperatorStep_1 - 각종 연산자 관련 코드. (0) | 2020.06.22 |
---|---|
개인 연습, 공부용 뼈대 코드 (0) | 2020.06.22 |
문법 정리- sequence type (0) | 2020.05.28 |