003. Python字符串处理-返回第一个单词(strip, split, re)

版权声明:欢迎联系我转载!欢迎交流分享!转载和分享请注明出处! https://blog.csdn.net/u014303046/article/details/82526166

First World

一、题目描述

要求:输入一个字符串,返回第一个单词

输入:字符串

输出:字符串

示例

first_word("Hello world") == "Hello"
first_word("greetings, friends") == "greetings"

注意

  • 字符串可能有逗号和句号
  • 字符串可能以字母或者句点、空格等开头
  • 单词可能包含特殊符号比如
  • 可能字符串只有一个单词

解题提示

可以用str.strip()和str.split()函数

也可以采用正则表达式模块。

二、解答示例

1. 正则表达式

import re


def first_word(text: str) -> str:
    return re.search("([\w']+)", text).group(1)

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("don't touch it") == "don't"
    assert first_word("greetings, friends") == "greetings"
    assert first_word("... and so on ...") == "and"
    assert first_word("hi") == "hi"
    assert first_word("Hello.World") == "Hello"
    print("Coding complete!")

2. strip & split

def first_word(text: str) -> str:
    tmp = text.strip('., ').split()[0].split(',')[0].split('.')[0]
    return tmp

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("don't touch it") == "don't"
    assert first_word("greetings, friends") == "greetings"
    assert first_word("... and so on ...") == "and"
    assert first_word("hi") == "hi"
    assert first_word("Hello.World") == "Hello"
    print("Coding complete!")

3. strip & split

先把逗号和句点替换成空格,然后再分割

def first_word(text: str) -> str:
    """
        returns the first word in a given text.
    """
    text = text.replace('.', ' ').replace(',', ' ').strip()
    return text.split()[0]


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

    # These "asserts" using for self-checking and not for auto-testing
    assert first_word("Hello world") == "Hello"
    assert first_word(" a word ") == "a"
    assert first_word("don't touch it") == "don't"
    assert first_word("greetings, friends") == "greetings"
    assert first_word("... and so on ...") == "and"
    assert first_word("hi") == "hi"
    print("Coding complete!")

4. lambda表达式

def first_word(text: str) -> str:
    return lambda t: ''.join([x, ' '][x in '.,'] for x in t).split()[0]


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

    # These "asserts" using for self-checking and not for auto-testing
    assert first_word("Hello world") == "Hello"
    assert first_word(" a word ") == "a"
    assert first_word("don't touch it") == "don't"
    assert first_word("greetings, friends") == "greetings"
    assert first_word("... and so on ...") == "and"
    assert first_word("hi") == "hi"
    print("Coding complete!")

猜你喜欢

转载自blog.csdn.net/u014303046/article/details/82526166