What's Your Name?
#
# Complete the 'print_full_name' function below.
#
# The function is expected to return a STRING.
# The function accepts following parameters:
# 1. STRING first
# 2. STRING last
#
def print_full_name(first, last):
print("Hello {} {}! You just delved into python.".format(first, last))
if __name__ == '__main__':
first_name = input()
last_name = input()
print_full_name(first_name, last_name)
#
# Complete the 'print_full_name' function below.
#
# The function is expected to return a STRING.
# The function accepts following parameters:
# 1. STRING first
# 2. STRING last
#
def print_full_name(first, last):
print("Hello {} {}! You just delved into python.".format(first, last))
if __name__ == '__main__':
first_name = input()
last_name = input()
print_full_name(first_name, last_name)
해설
포맷팅 함수를 사용하여 '성'과 '이름'을 출력하는 코드이다.
format
format함수는 문자열의 포맷을 지정하는 함수이다.
직접 값을 대입하거나 간접적으로 변수를 통해 값을 대입할 수 있다.
# 직접 값을 대입하기
## 문자열 바로 대입하기
>>> "Hello {0} century".format("twenty one")
'Hello twenty one century' # 문자열의 {0} 항목이 twenty one라는 문자열로 바뀌었다.
## 숫자 값을 가진 변수로 대입하기
>>> number = 21
>>> "Hello {0} century".format(number)
'Hello 21e century' # 문자열의 {0} 항목이 number 변수 값인 21으로 바뀌었다.
# 변수를 통해 대입하기
# 변수 = input()이 되는 경우, 좀 더 효율적으로 사용가능하다.
>>> first = "Steve"
>>> Last = "jobs"
print("Hello {} {}! You just delved into python.".format(first, last))
'Develop > hackerrank.com' 카테고리의 다른 글
HackerRank Python Solution / Find a string (0) | 2022.01.17 |
---|---|
HackerRank Python Solution / Mutations (0) | 2022.01.17 |
HackerRank Python Solution / String Split and Join (0) | 2022.01.17 |
HackerRank Python Solution / sWAP cASE (0) | 2022.01.11 |
HackerRank Python Solution / Tuples (0) | 2022.01.11 |