프로그래머스/Lv.1
서울에서 김서방 찾기 - Python, format
아찌방
2024. 11. 27. 23:09
https://school.programmers.co.kr/learn/courses/30/lessons/12919
프로그래머스
SW개발자를 위한 평가, 교육, 채용까지 Total Solution을 제공하는 개발자 성장을 위한 베이스캠프
programmers.co.kr
풀이 - 나의 생각
format의 사용법을 알기 위해 기록
예시
name = "Alice"
age = 30
city = "Wonderland"
# 기본 포매팅
print("{} is {} years old.".format(name, age))
# 순서 지정
print("{0} is {1} years old. {0} lives in {2}.".format(name, age, city))
# 키워드 인자 사용
print("{name} is {age} years old. {name} lives in {city}.".format(name=name, age=age, city=city))
# 숫자 포맷팅
pi = 3.14159
print("Pi to two decimal places: {:.2f}".format(pi))
# 중괄호
print("{{}}".format())
# 출력
# Alice is 30 years old.
# Alice is 30 years old. Alice lives in Wonderland.
# Alice is 30 years old. Alice lives in Wonderland.
# Pi to two decimal places: 3.14
# {}
코드
def solution(seoul):
for idx, name in enumerate(seoul):
if name == "Kim":
answer = "김서방은 "+str(idx)+"에 있다"
return answer
def solution(seoul):
return "김서방은 {}에 있다".format(seoul.index("Kim"))
728x90