如何从python中的字符串中提取单词?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/42854100/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 22:12:21  来源:igfitidea点击:

How to extract words from the string in python?

python

提问by neha pawar

I have strings of the form

我有形式的字符串

 sent="Software Development = 1831".

I want to extract only words from the string i.e. "Software Development". How I can extract this in Python.

我只想从字符串中提取单词,即“软件开发”。我如何在 Python 中提取它。

回答by Harsha Biyani

You can try split:

你可以试试split

>>> sent="Software Development = 1831"
>>> sent.split("=")[0]
'Software Development '

回答by maiky_forrester

>>> str="Software Development = 1831"

>>> str.split()
['Software', 'Development', '=', '1831']

or 

>>> str.split("=")
['Software Development ', ' 1831']

回答by Deepak S

Initially you have spaces between '=', hence your code should look like this

最初你之间有空格'=',因此你的代码应该是这样的

sent="Software Development = 1831"
answer=sent.split(" = ")[0] # space before and after =