Python/python 기초

Format

DS지니 2021. 4. 2. 21:04
728x90
반응형

방법 1.

fruit = "orange"
txt = "My favorite fruite is {}"
print(txt.format(fruit))

> My favorite fruite is orange

 

 

quantity = 3
itemno = 567
price = 49.95
myorder = "I want {} pieces of item {} for {} dollars."
print(myorder.format(quantity, itemno, price))

> I want 3 pieces of item 567 for 49.95 dollars

 

 

quantity = 3
itemno = 567
price = 49.95
myorder = "I want to pay {2} dollars for {0} pieces of item {1}."
print(myorder.format(quantity, itemno, price))

> I want to pay 49.95 dollars for 3 pieces of item 567.

 

 

방법 2.

fruit = "orange"

print(f"My favorite fruite is {fruit}")
728x90
반응형

'Python > python 기초' 카테고리의 다른 글

Python Boolean 평가하기  (0) 2021.04.02
Escape Characters  (0) 2021.04.02
문자열 수정(Modify Strings)  (0) 2021.04.02
내장 데이터 유형 (Built-in Datatypes)  (0) 2021.04.02
변수 할당하는 방법 3가지  (0) 2021.04.02