首页 > 解决方案 > Python在字符串中查找首字母大写后跟字母数字字符的单词

问题描述

输入:"We are Testing."

输出:['We', 'Testing']

输入:"Hello1 Hello2 Hello3"

输出:['Hello1', 'Hello2', 'Hello3']

如果字符串中没有这样的单词,则返回 'None'

有没有一种有效的方法来解决这个问题,我尝试拆分字符串,但似乎效果不佳。

标签: pythonpython-3.x

解决方案


函数将字符串作为参数并对其进行处理

def finder(string):
    words_list = string.split(' ')

    # Check if the first letter is uppercase and is alphanumerical
    # Else remove the word from list
    words_list = [i for i in words_list if i == i.capitalize() and i.isalnum()]

    return words_list

推荐阅读