Hướng dẫn how to extract a word from a string in python - cách trích xuất một từ trong chuỗi trong python

Đôi khi chúng ta đi qua các tình huống mà chúng ta yêu cầu để có được tất cả các từ có trong chuỗi, đây có thể là một nhiệm vụ tẻ nhạt được thực hiện bằng phương pháp gốc. Do đó có tốc ký để thực hiện nhiệm vụ này luôn hữu ích. Ngoài ra, bài viết này cũng bao gồm các trường hợp trong đó các dấu chấm câu phải bị bỏ qua. Phương pháp nếu người ta muốn hoàn thành nhiệm vụ cụ thể này. Nhưng nhược điểm là nó thất bại trong các trường hợp chuỗi chứa dấu chấm câu. & NBSP;
Method #1 : Using split[] 
Using the split function, we can split the string into a list of words and this is the most generic and recommended method if one wished to accomplish this particular task. But the drawback is that it fails in cases the string contains punctuation marks.
 

Python3

test_string =

String from which word has to be searched is:
I am a python programmer and I am writing this code for pythonforbeginners.com
word to be extracted from string:
writing
Extracted word is:
writing
0

String from which word has to be searched is:
I am a python programmer and I am writing this code for pythonforbeginners.com
word to be extracted from string:
writing
Extracted word is:
writing
1
String from which word has to be searched is:
I am a python programmer and I am writing this code for pythonforbeginners.com
word to be extracted from string:
writing
Extracted word is:
writing
2
String from which word has to be searched is:
I am a python programmer and I am writing this code for pythonforbeginners.com
word to be extracted from string:
writing
Extracted word is:
writing
3 ________ 14 & nbsp;
String from which word has to be searched is:
I am a python programmer and I am writing this code for pythonforbeginners.com
word to be extracted from string:
writing
Extracted word is:
writing
5

String from which word has to be searched is:
I am a python programmer and I am writing this code for pythonforbeginners.com
word to be extracted from string:
writing
Extracted word is:
writing
6=
String from which word has to be searched is:
I am a python programmer and I am writing this code for pythonforbeginners.com
word to be extracted from string:
writing
Extracted word is:
writing
8

String from which word has to be searched is:
I am a python programmer and I am writing this code for pythonforbeginners.com
word to be extracted from string:
writing
Extracted word is:
writing
1
String from which word has to be searched is:
I am a python programmer and I am writing this code for pythonforbeginners.com
word to be extracted from string:
writing
Extracted word is:
writing
2
search_string= "I am a python programmer and I am writing this code for pythonforbeginners.com"
print["String from which word has to be searched is:"]
print[search_string]
print["word to be extracted from string:"]
word="writing"
print[word]
#calculate length of the word
lword=len[word]
start_index=search_string.find[word]
print["start index of the word in string is:"]
print[start_index]
extracted_string= search_string[start_index:start_index+lword]
print["Extracted word is:"]
print[extracted_string]
1 ________ 14 & nbsp;
search_string= "I am a python programmer and I am writing this code for pythonforbeginners.com"
print["String from which word has to be searched is:"]
print[search_string]
print["word to be extracted from string:"]
word="writing"
print[word]
#calculate length of the word
lword=len[word]
start_index=search_string.find[word]
print["start index of the word in string is:"]
print[start_index]
extracted_string= search_string[start_index:start_index+lword]
print["Extracted word is:"]
print[extracted_string]
3
search_string= "I am a python programmer and I am writing this code for pythonforbeginners.com"
print["String from which word has to be searched is:"]
print[search_string]
print["word to be extracted from string:"]
word="writing"
print[word]
#calculate length of the word
lword=len[word]
start_index=search_string.find[word]
print["start index of the word in string is:"]
print[start_index]
extracted_string= search_string[start_index:start_index+lword]
print["Extracted word is:"]
print[extracted_string]
4

Đầu ra: & nbsp; chuỗi gốc là: geeksforgeek là cổng thông tin khoa học máy tính tốt nhất & nbsp; danh sách các từ là: [' ; 
The original string is : Geeksforgeeks is best Computer Science Portal 
The list of words is : [‘Geeksforgeeks’, ‘is’, ‘best’, ‘Computer’, ‘Science’, ‘Portal’] 
 

& nbsp; & nbsp; Phương pháp #2: Sử dụng regex [findall []] & nbsp; Trong các trường hợp chứa tất cả các ký tự đặc biệt và dấu chấm câu, như đã thảo luận ở trên, phương pháp tìm kiếm thông thường Biểu thức để thực hiện nhiệm vụ này. Hàm Findall Trả về danh sách sau khi lọc chuỗi và trích xuất các từ bỏ qua các dấu chấm câu. & nbsp;
Method #2 : Using regex[ findall[] ] 
In the cases which contain all the special characters and punctuation marks, as discussed above, the conventional method of finding words in string using split can fail and hence requires regular expressions to perform this task. findall function returns the list after filtering the string and extracting words ignoring punctuation marks.
 

Python3

search_string= "I am a python programmer and I am writing this code for pythonforbeginners.com"
print["String from which word has to be searched is:"]
print[search_string]
print["word to be extracted from string:"]
word="writing"
print[word]
#calculate length of the word
lword=len[word]
start_index=search_string.find[word]
print["start index of the word in string is:"]
print[start_index]
extracted_string= search_string[start_index:start_index+lword]
print["Extracted word is:"]
print[extracted_string]
5
search_string= "I am a python programmer and I am writing this code for pythonforbeginners.com"
print["String from which word has to be searched is:"]
print[search_string]
print["word to be extracted from string:"]
word="writing"
print[word]
#calculate length of the word
lword=len[word]
start_index=search_string.find[word]
print["start index of the word in string is:"]
print[start_index]
extracted_string= search_string[start_index:start_index+lword]
print["Extracted word is:"]
print[extracted_string]
6

test_string =

search_string= "I am a python programmer and I am writing this code for pythonforbeginners.com"
print["String from which word has to be searched is:"]
print[search_string]
print["word to be extracted from string:"]
word="writing"
print[word]
#calculate length of the word
lword=len[word]
start_index=search_string.find[word]
print["start index of the word in string is:"]
print[start_index]
extracted_string= search_string[start_index:start_index+lword]
print["Extracted word is:"]
print[extracted_string]
9

String from which word has to be searched is:
I am a python programmer and I am writing this code for pythonforbeginners.com
word to be extracted from string:
writing
Extracted word is:
writing
1
String from which word has to be searched is:
I am a python programmer and I am writing this code for pythonforbeginners.com
word to be extracted from string:
writing
Extracted word is:
writing
2
String from which word has to be searched is:
I am a python programmer and I am writing this code for pythonforbeginners.com
word to be extracted from string:
writing
Extracted word is:
writing
3 ________ 14 & nbsp;
String from which word has to be searched is:
I am a python programmer and I am writing this code for pythonforbeginners.com
word to be extracted from string:
writing
Extracted word is:
writing
5

String from which word has to be searched is:
I am a python programmer and I am writing this code for pythonforbeginners.com
word to be extracted from string:
writing
Extracted word is:
writing
6=
String from which word has to be searched is:
I am a python programmer and I am writing this code for pythonforbeginners.com
word to be extracted from string:
writing
Extracted word is:
writing
8

String from which word has to be searched is:
I am a python programmer and I am writing this code for pythonforbeginners.com
word to be extracted from string:
writing
Extracted word is:
writing
1
String from which word has to be searched is:
I am a python programmer and I am writing this code for pythonforbeginners.com
word to be extracted from string:
writing
Extracted word is:
writing
2
search_string= "I am a python programmer and I am writing this code for pythonforbeginners.com"
print["String from which word has to be searched is:"]
print[search_string]
print["word to be extracted from string:"]
word="writing"
print[word]
#calculate length of the word
lword=len[word]
start_index=search_string.find[word]
print["start index of the word in string is:"]
print[start_index]
extracted_string= search_string[start_index:start_index+lword]
print["Extracted word is:"]
print[extracted_string]
1 ________ 14 & nbsp;
search_string= "I am a python programmer and I am writing this code for pythonforbeginners.com"
print["String from which word has to be searched is:"]
print[search_string]
print["word to be extracted from string:"]
word="writing"
print[word]
#calculate length of the word
lword=len[word]
start_index=search_string.find[word]
print["start index of the word in string is:"]
print[start_index]
extracted_string= search_string[start_index:start_index+lword]
print["Extracted word is:"]
print[extracted_string]
3
search_string= "I am a python programmer and I am writing this code for pythonforbeginners.com"
print["String from which word has to be searched is:"]
print[search_string]
print["word to be extracted from string:"]
word="writing"
print[word]
#calculate length of the word
lword=len[word]
start_index=search_string.find[word]
print["start index of the word in string is:"]
print[start_index]
extracted_string= search_string[start_index:start_index+lword]
print["Extracted word is:"]
print[extracted_string]
4

Đầu ra: & nbsp; chuỗi gốc là: geeksforgeek là cổng thông tin khoa học máy tính tốt nhất & nbsp; danh sách các từ là: [' ; 
The original string is : Geeksforgeeks, is best @# Computer Science Portal.!!! 
The list of words is : [‘Geeksforgeeks’, ‘is’, ‘best’, ‘Computer’, ‘Science’, ‘Portal’] 
 

& nbsp; & nbsp; Phương pháp #2: Sử dụng regex [findall []] & nbsp; Trong các trường hợp chứa tất cả các ký tự đặc biệt và dấu chấm câu, như đã thảo luận ở trên, phương pháp tìm kiếm thông thường Biểu thức để thực hiện nhiệm vụ này. Hàm Findall Trả về danh sách sau khi lọc chuỗi và trích xuất các từ bỏ qua các dấu chấm câu. & nbsp;
Method #3 : Using regex[] + string.punctuation 
This method also used regular expressions, but string function of getting all the punctuations is used to ignore all the punctuation marks and get the filtered result string.
 

Python3

search_string= "I am a python programmer and I am writing this code for pythonforbeginners.com"
print["String from which word has to be searched is:"]
print[search_string]
print["word to be extracted from string:"]
word="writing"
print[word]
#calculate length of the word
lword=len[word]
start_index=search_string.find[word]
print["start index of the word in string is:"]
print[start_index]
extracted_string= search_string[start_index:start_index+lword]
print["Extracted word is:"]
print[extracted_string]
5
search_string= "I am a python programmer and I am writing this code for pythonforbeginners.com"
print["String from which word has to be searched is:"]
print[search_string]
print["word to be extracted from string:"]
word="writing"
print[word]
#calculate length of the word
lword=len[word]
start_index=search_string.find[word]
print["start index of the word in string is:"]
print[start_index]
extracted_string= search_string[start_index:start_index+lword]
print["Extracted word is:"]
print[extracted_string]
6

test_string =

search_string= "I am a python programmer and I am writing this code for pythonforbeginners.com"
print["String from which word has to be searched is:"]
print[search_string]
print["word to be extracted from string:"]
word="writing"
print[word]
#calculate length of the word
lword=len[word]
start_index=search_string.find[word]
print["start index of the word in string is:"]
print[start_index]
extracted_string= search_string[start_index:start_index+lword]
print["Extracted word is:"]
print[extracted_string]
9

test_string =

search_string= "I am a python programmer and I am writing this code for pythonforbeginners.com"
print["String from which word has to be searched is:"]
print[search_string]
print["word to be extracted from string:"]
word="writing"
print[word]
#calculate length of the word
lword=len[word]
start_index=search_string.find[word]
print["start index of the word in string is:"]
print[start_index]
extracted_string= search_string[start_index:start_index+lword]
print["Extracted word is:"]
print[extracted_string]
9

String from which word has to be searched is:
I am a python programmer and I am writing this code for pythonforbeginners.com
word to be extracted from string:
writing
Extracted word is:
writing
1
String from which word has to be searched is:
I am a python programmer and I am writing this code for pythonforbeginners.com
word to be extracted from string:
writing
Extracted word is:
writing
2
String from which word has to be searched is:
I am a python programmer and I am writing this code for pythonforbeginners.com
word to be extracted from string:
writing
Extracted word is:
writing
3 ________ 14 & nbsp;
String from which word has to be searched is:
I am a python programmer and I am writing this code for pythonforbeginners.com
word to be extracted from string:
writing
Extracted word is:
writing
5

String from which word has to be searched is:
I am a python programmer and I am writing this code for pythonforbeginners.com
word to be extracted from string:
writing
Extracted word is:
writing
6=
String from which word has to be searched is:
I am a python programmer and I am writing this code for pythonforbeginners.com
word to be extracted from string:
writing
Extracted word is:
writing
8

String from which word has to be searched is:
I am a python programmer and I am writing this code for pythonforbeginners.com
word to be extracted from string:
writing
Extracted word is:
writing
1
String from which word has to be searched is:
I am a python programmer and I am writing this code for pythonforbeginners.com
word to be extracted from string:
writing
Extracted word is:
writing
2
search_string= "I am a python programmer and I am writing this code for pythonforbeginners.com"
print["String from which word has to be searched is:"]
print[search_string]
print["word to be extracted from string:"]
word="writing"
print[word]
#calculate length of the word
lword=len[word]
start_index=search_string.find[word]
print["start index of the word in string is:"]
print[start_index]
extracted_string= search_string[start_index:start_index+lword]
print["Extracted word is:"]
print[extracted_string]
1 ________ 14 & nbsp;
search_string= "I am a python programmer and I am writing this code for pythonforbeginners.com"
print["String from which word has to be searched is:"]
print[search_string]
print["word to be extracted from string:"]
word="writing"
print[word]
#calculate length of the word
lword=len[word]
start_index=search_string.find[word]
print["start index of the word in string is:"]
print[start_index]
extracted_string= search_string[start_index:start_index+lword]
print["Extracted word is:"]
print[extracted_string]
3
search_string= "I am a python programmer and I am writing this code for pythonforbeginners.com"
print["String from which word has to be searched is:"]
print[search_string]
print["word to be extracted from string:"]
word="writing"
print[word]
#calculate length of the word
lword=len[word]
start_index=search_string.find[word]
print["start index of the word in string is:"]
print[start_index]
extracted_string= search_string[start_index:start_index+lword]
print["Extracted word is:"]
print[extracted_string]
4

Đầu ra: & nbsp; chuỗi gốc là: geeksforgeek là cổng thông tin khoa học máy tính tốt nhất & nbsp; danh sách các từ là: [' ; 
The original string is : Geeksforgeeks, is best @# Computer Science Portal.!!! 
The list of words is : [‘Geeksforgeeks’, ‘is’, ‘best’, ‘Computer’, ‘Science’, ‘Portal’] 
 


Trong khi xử lý dữ liệu văn bản, đôi khi chúng ta phải tìm kiếm các từ của các từ cụ thể trong văn bản và trích xuất các từ cụ thể. Trong hướng dẫn này, chúng tôi sẽ tìm hiểu về các phương pháp khác nhau để trích xuất một từ cụ thể từ một chuỗi trong Python bằng các phương thức chuỗi in tìm và các biểu thức thông thường. Vì vậy, hãy để Lặn đi sâu vào nó.

Nếu chúng ta biết vị trí chính xác của từ được trích xuất từ ​​chuỗi, chúng ta có thể thực hiện thao tác cắt lát trên chuỗi để trích xuất từ ​​mong muốn từ chuỗi như hình bên dưới.


search_string= "I am a python programmer and I am writing this code for pythonforbeginners.com"
print["String from which word has to be searched is:"]
print[search_string]
print["word to be extracted from string:"]
word="writing"
print[word]
#calculate length of the word
lword=len[word]
#suppose we already know the starting index of word writing i.e. 34
extracted_string= search_string[34:34+lword]
print["Extracted word is:"]
print[extracted_string]

Output:

String from which word has to be searched is:
I am a python programmer and I am writing this code for pythonforbeginners.com
word to be extracted from string:
writing
Extracted word is:
writing

Trích xuất một từ cụ thể từ một chuỗi bằng phương thức find [].

Nếu chúng ta muốn trích xuất một từ cụ thể từ chuỗi và chúng ta không biết vị trí chính xác của từ, trước tiên chúng ta có thể tìm thấy vị trí của từ bằng cách sử dụng


String from which word has to be searched is:
I am a python programmer and I am writing this code for pythonforbeginners.com
word to be extracted from string:
writing
start index of the word in string is:
34
Extracted word is:
writing
3Method và sau đó chúng ta có thể trích xuất từ ​​bằng cách sử dụng cắt chuỗi.

Phương thức


String from which word has to be searched is:
I am a python programmer and I am writing this code for pythonforbeginners.com
word to be extracted from string:
writing
start index of the word in string is:
34
Extracted word is:
writing
4 Khi được gọi trên bất kỳ chuỗi nào, lấy chuỗi để được tìm kiếm làm tham số và đưa ra đầu ra vị trí xuất hiện đầu tiên của chuỗi đầu vào sẽ được tìm kiếm. Nếu chuỗi được tìm kiếm không có mặt, phương thức

String from which word has to be searched is:
I am a python programmer and I am writing this code for pythonforbeginners.com
word to be extracted from string:
writing
start index of the word in string is:
34
Extracted word is:
writing
4 trả về -1.

Sau khi tìm thấy vị trí của từ được trích xuất bằng phương pháp


String from which word has to be searched is:
I am a python programmer and I am writing this code for pythonforbeginners.com
word to be extracted from string:
writing
start index of the word in string is:
34
Extracted word is:
writing
4, chúng ta chỉ có thể trích xuất nó bằng hoạt động lát cắt như sau.

search_string= "I am a python programmer and I am writing this code for pythonforbeginners.com"
print["String from which word has to be searched is:"]
print[search_string]
print["word to be extracted from string:"]
word="writing"
print[word]
#calculate length of the word
lword=len[word]
start_index=search_string.find[word]
print["start index of the word in string is:"]
print[start_index]
extracted_string= search_string[start_index:start_index+lword]
print["Extracted word is:"]
print[extracted_string]

Output:

String from which word has to be searched is:
I am a python programmer and I am writing this code for pythonforbeginners.com
word to be extracted from string:
writing
start index of the word in string is:
34
Extracted word is:
writing

Sử dụng phương thức index [].

Nếu chúng ta không biết vị trí chính xác của từ được trích xuất, chúng ta cũng có thể sử dụng chuỗi


String from which word has to be searched is:
I am a python programmer and I am writing this code for pythonforbeginners.com
word to be extracted from string:
writing
start index of the word in string is:
34
Extracted word is:
writing
7Method để tìm ra vị trí chính xác của từ và sau đó chúng ta có thể sử dụng cắt lát để trích xuất từ.


String from which word has to be searched is:
I am a python programmer and I am writing this code for pythonforbeginners.com
word to be extracted from string:
writing
start index of the word in string is:
34
Extracted word is:
writing
7Method Khi được gọi trên bất kỳ chuỗi nào, lấy chuỗi để được tìm kiếm làm tham số và đưa ra vị trí xuất hiện đầu tiên của chuỗi đầu vào sẽ được tìm kiếm. Nếu chuỗi được tìm kiếm không có mặt,

String from which word has to be searched is:
I am a python programmer and I am writing this code for pythonforbeginners.com
word to be extracted from string:
writing
start index of the word in string is:
34
Extracted word is:
writing
9 ném một ngoại lệ. Vì lý do này, chúng tôi sẽ phải sử dụng Python, hãy thử ngoại trừ để xử lý các ngoại lệ trong khi sử dụng phương thức

String from which word has to be searched is:
I am a python programmer and I am writing this code for pythonforbeginners.com
word to be extracted from string:
writing
start index of the word in string is:
34
Extracted word is:
writing
9.

Chúng ta có thể trích xuất một từ cụ thể từ một chuỗi trong python bằng phương thức index [] và cắt chuỗi như sau.


search_string= "I am a python programmer and I am writing this code for pythonforbeginners.com"
print["String from which word has to be searched is:"]
print[search_string]
print["word to be extracted from string:"]
word="writing"
print[word]
#calculate length of the word
lword=len[word]
try:
    
    start_index=search_string.index[word]
    print["start index of the word in string is:"]
    print[start_index]
    extracted_string= search_string[start_index:start_index+lword]
    print["Extracted word is:"]
    print[extracted_string]
except:
    print["word not found"]

Output:

String from which word has to be searched is:
I am a python programmer and I am writing this code for pythonforbeginners.com
word to be extracted from string:
writing
start index of the word in string is:
34
Extracted word is:
writing

Sử dụng các biểu thức chính quy để trích xuất bất kỳ từ cụ thể nào

Chúng ta có thể sử dụng các biểu thức thông thường trong Python để trích xuất các từ cụ thể từ một chuỗi. Chúng ta có thể sử dụng phương thức test_string 1 từ mô -đun

search_string= "I am a python programmer and I am writing this code for pythonforbeginners.com"
print["String from which word has to be searched is:"]
print[search_string]
print["word to be extracted from string:"]
word="writing"
print[word]
#calculate length of the word
lword=len[word]
start_index=search_string.find[word]
print["start index of the word in string is:"]
print[start_index]
extracted_string= search_string[start_index:start_index+lword]
print["Extracted word is:"]
print[extracted_string]
6 để tìm sự xuất hiện đầu tiên của từ và sau đó chúng ta có thể có được từ bằng cách sử dụng cắt.

Phương thức test_string 3 & NBSP; Sẽ lấy từ để được trích xuất ở dạng biểu thức chính quy và chuỗi làm đầu vào và trả về test_string 4 chứa chỉ số bắt đầu và kết thúc của từ. Nếu không tìm thấy từ đã cho, test_string 3 sẽ trả về test_string 6. Sau khi nhận được các chỉ số của từ được trích xuất, chúng ta có thể trích xuất nó bằng cách sử dụng cắt chuỗi như hình bên dưới.

import re
search_string= "I am a python programmer and I am writing this code for pythonforbeginners.com"
print["String from which word has to be searched is:"]
print[search_string]
print["word to be extracted from string:"]
word=r"writing"
print[word]
#calculate length of the word
lword=len[word]
start_index=re.search[word,search_string].start[]
print["start index of the word in string is:"]
print[start_index]
extracted_string= search_string[start_index:start_index+lword]
print["Extracted word is:"]
print[extracted_string]

Output:


String from which word has to be searched is:
I am a python programmer and I am writing this code for pythonforbeginners.com
word to be extracted from string:
writing
start index of the word in string is:
34
Extracted word is:
writing

Sự kết luận

Trong bài viết này, chúng ta đã thấy cách tìm bất kỳ từ cụ thể nào trong một chuỗi bằng các phương thức chuỗi khác nhau và các biểu thức chính quy và sau đó in từ bằng cách sử dụng cắt chuỗi trong Python. Chúng ta cũng có thể sử dụng thao tác phân chia chuỗi Python khi chúng ta phải tìm kiếm nếu từ có hay không, vì các từ là không gian tách biệt. Hãy theo dõi các bài viết nhiều thông tin hơn.

Khuyến nghị đào tạo Python

Khóa học: Python 3 cho người mới bắt đầu

Hơn 15 giờ nội dung video với hướng dẫn hướng dẫn cho người mới bắt đầu. Tìm hiểu làm thế nào để tạo các ứng dụng trong thế giới thực và làm chủ những điều cơ bản.

Bài Viết Liên Quan

Chủ Đề