본문 바로가기

Python_Matter/[Check_IO]Elementary

First Word (simplified)

반응형

Quiz>

You are given a string where you have to find its first word.

 

This is a simplified version of the First Word mission.

 

Input string consists of only english letters and spaces.

There aren’t any spaces at the beginning and the end of the string.

 

Input:

A string.

 

Output:

A string.

 

Example:

first_word("Hello world") == "Hello"

 

How it is used: The first word is a command in a command line.

 

Precondition: Text can contain a-z, A-Z and spaces.

 

def first_word(text: str) -> str:
    """
        returns the first word in a given text.
    """
    # your code here
    return text[0:2]


if __name__ == '__main__':
    print("Example:")
    print(first_word("Hello world"))
    
    # These "asserts" are used for self-checking and not for an auto-testing
    assert first_word("Hello world") == "Hello"
    assert first_word("a word") == "a"
    assert first_word("hi") == "hi"
    print("Coding complete? Click 'Check' to earn cool rewards!")

 

Solve>

1. strip하고 split를 사용하여 문자를 잘라낸다.

   단, strip와 split 사용 위치를 잘 체크해야된다.

def first_word(text: str):
    text = text.strip().split()

 

2. 리턴값은 입력받은 값에 맨 처음 단어만 출력해야되므로 인덱싱 번호는 0을 출력한다.

def first_word(text: str):
    text = text.strip().split()
    return text[0]

 

Code>

def first_word(text: str):
    text = text.strip().split()
    return text[0]

 

Example>

if __name__ == '__main__':
    print("Example:")
    print(first_word("Hello world"))

 

Result>

Example:
Hello

반응형

'Python_Matter > [Check_IO]Elementary' 카테고리의 다른 글

Correct Sentence  (0) 2020.04.11
Fizz Buzz  (0) 2020.04.11
Say Hi  (0) 2020.04.11
Easy Unpack  (0) 2020.04.10
Multiply (Intro)  (0) 2020.04.10