티스토리 뷰

728x90

Capitalize!

You are asked to ensure that the first and last names of people begin with a capital letter in their passports. For example, alison heck should be capitalised correctly as Alison Heck.

Given a full name, your task is to capitalize the name appropriately.

Input Format

A single line of input containing the full name, S.

Constraints

  • The string consists of alphanumeric characters and spaces.

Note: in a word only the first character is capitalized. Example 12abc when capitalized remains 12abc.

Output Format

Print the capitalized string, S.

Sample Input

chris alan

Sample Output

Chris Alan

 

문제해석

입력되는 문자열의 첫 번째 알파벳을 대문자로 변환하면 된다. 주의할 점은 chris alan과 같이 입력이 된다면 chris의 c와 alan의 a 2개의 문자를 변환해야 된다는 것이다.

 

문제풀이

파이썬 string method에서 지원하는 capitalize() 함수를 사용하면 첫 번째 문자를 대문자로 변환하는 것이 가능하다. 하지만 이 함수는 전체 문자열에서 첫 번째 문자에만 적용되므로 문제에서 원하는 것과 조금 다르다. 그래서 입력받은 문자열을 공백 기준으로 split한 다음 각 문자열에 대해 capitalize()를 적용하고 다시 join()하여 원하는 결과가 나오도록 하였다. 

#!/bin/python3

import math
import os
import random
import re
import sys

# Complete the solve function below.
def solve(s):
    sl = s.split(" ")
    for i in range(len(sl)):        
        sl[i] = sl[i].capitalize()
    
    return " ".join(sl)

if __name__ == '__main__':
    fptr = open(os.environ['OUTPUT_PATH'], 'w')

    s = input()

    result = solve(s)

    fptr.write(result + '\n')

    fptr.close()

 

 

728x90
LIST
댓글
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
«   2024/04   »
1 2 3 4 5 6
7 8 9 10 11 12 13
14 15 16 17 18 19 20
21 22 23 24 25 26 27
28 29 30
글 보관함