003. Python字符串处理-返回第一个单词(strip, split, re)
2018-09-08 09:06
471 查看
版权声明:欢迎联系我转载!欢迎交流分享!转载和分享请注明出处! 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表达式
4000 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!")
相关文章推荐
- 第16周项目2.5 去除句子中第一个单词的空格,返回处理后的字符串 用指针做形参
- python字符串切割:str.split()和re.split()对比
- python基础总结三:字符串的一些常用函数(count、replace、split、capitalize、title、upper、ljust、strip等)及操作
- python学习笔记-使用re模板在字符串中查找所有单词
- python(5)字符串处理 (sub,replace,find,index,upper,strip)
- Python strip lstrip rstrip使用方法(字符串处理空格)
- python字符串处理 join split replace的使用方法
- python | strip() 和split() 移除字符串末尾数字(正则表达式)
- SqlServer:根据指定分隔符,把传入字符串分隔处理后,返回数据表,函数功能类似.NET的string.Split()
- C++调用 python 函数及返回值的处理【元组,字符串...】
- python字符串split()、join()、strip()函数的总结
- Python3基础 os.path.dirname 对一个路径字符串进行处理 返回路径
- python中使用re.sub更换倒数第一个字符串
- Python3基础 str capitalize 返回一个新的字符串,它的第一个字母大写
- python字符串split(),join(),strip()函数及网易笔试 回文序列
- Python strip lstrip rstrip使用方法(字符串处理空格)
- 第十六周 项目二--用指针玩字符串(2.5去除句子中第一个单词前的空格并返回--指针作形参)
- 第十六周项目二用指针玩字符串任务五:去除句子中第一个单词前的空格,返回去除了空格的字符串(用指针作形参)
- python多字符串分割(re.split)
- Python strip lstrip rstrip使用方法(字符串处理空格)