Trích xuất chuỗi con từ chuỗi Python

Cho một chuỗi và hai chuỗi con, hãy viết chương trình Python để trích xuất chuỗi giữa hai chuỗi con tìm được.  

Thí dụ

Đầu vào. test_str = “Gfg là tốt nhất cho chuyên viên máy tính và CS”, sub1 = “là”, sub2 = “và”

đầu ra. tốt nhất cho chuyên viên máy tính

Giải trình. tốt nhất cho chuyên viên máy tính là giữa là và 'và'

Đầu vào. test_str = “Gfg là tốt nhất cho chuyên viên máy tính và CS”, sub1 = “dành cho”, sub2 = “và”

đầu ra. chuyên viên máy tính

Giải trình. chuyên viên máy tính là giữa cho và 'và'

Sử dụng vòng lặp index[] + để trích xuất chuỗi giữa hai chuỗi con

Trong trường hợp này, chúng tôi lấy các chỉ số của cả hai chuỗi con bằng cách sử dụng chỉ mục [], sau đó một vòng lặp được sử dụng để lặp lại trong chỉ mục để tìm chuỗi cần thiết giữa chúng

Python3




The original string is : Gfg is best for geeks and CS
The extracted string : best for geeks 
54

The original string is : Gfg is best for geeks and CS
The extracted string : best for geeks 
55

The original string is : Gfg is best for geeks and CS
The extracted string : best for geeks 
56

 

The original string is : Gfg is best for geeks and CS
The extracted string : best for geeks 
57

The original string is : Gfg is best for geeks and CS
The extracted string : best for geeks 
58
The original string is : Gfg is best for geeks and CS
The extracted string : best for geeks 
0
The original string is : Gfg is best for geeks and CS
The extracted string : best for geeks 
1

 

The original string is : Gfg is best for geeks and CS
The extracted string : best for geeks 
2

The original string is : Gfg is best for geeks and CS
The extracted string : best for geeks 
3______04
The original string is : Gfg is best for geeks and CS
The extracted string : best for geeks 
5
The original string is : Gfg is best for geeks and CS
The extracted string : best for geeks 
6
The original string is : Gfg is best for geeks and CS
The extracted string : best for geeks 
7
The original string is : Gfg is best for geeks and CS
The extracted string : best for geeks 
8

 

The original string is : Gfg is best for geeks and CS
The extracted string : best for geeks 
9

The original string is : Gfg is best for geeks and CS
The extracted string : best for geeks 
0
The original string is : Gfg is best for geeks and CS
The extracted string : best for geeks 
0
The original string is : Gfg is best for geeks and CS
The extracted string : best for geeks 
2

The original string is : Gfg is best for geeks and CS
The extracted string : best for geeks 
3
The original string is : Gfg is best for geeks and CS
The extracted string : best for geeks 
0
The original string is : Gfg is best for geeks and CS
The extracted string : best for geeks 
5

 

The original string is : Gfg is best for geeks and CS
The extracted string : best for geeks 
6

The original string is : Gfg is best for geeks and CS
The extracted string : best for geeks 
7
The original string is : Gfg is best for geeks and CS
The extracted string : best for geeks 
0
The original string is : Gfg is best for geeks and CS
The extracted string : best for geeks 
9

The original string is : Gfg is best for geeks and CS
The extracted string : best for geeks 
0
The original string is : Gfg is best for geeks and CS
The extracted string : best for geeks 
0
The original string is : Gfg is best for geeks and CS
The extracted string : best for geeks 
2

 

The original string is : Gfg is best for geeks and CS
The extracted string : best for geeks 
3
The original string is : Gfg is best for geeks and CS
The extracted string : best for geeks 
0
The original string is : Gfg is best for geeks and CS
The extracted string : best for geeks 
5

The original string is : Gfg is best for geeks and CS
The extracted string : best for geeks 
6

The original string is : Gfg is best for geeks and CS
The extracted string : best for geeks 
7
The original string is : Gfg is best for geeks and CS
The extracted string : best for geeks 
8
The original string is : Gfg is best for geeks and CS
The extracted string : best for geeks 
9
The original string is : Gfg is best for geeks and CS
The extracted string : best for geeks 
00
The original string is : Gfg is best for geeks and CS
The extracted string : best for geeks 
01
The original string is : Gfg is best for geeks and CS
The extracted string : best for geeks 
6
The original string is : Gfg is best for geeks and CS
The extracted string : best for geeks 
03
The original string is : Gfg is best for geeks and CS
The extracted string : best for geeks 
04
The original string is : Gfg is best for geeks and CS
The extracted string : best for geeks 
6
The original string is : Gfg is best for geeks and CS
The extracted string : best for geeks 
06
The original string is : Gfg is best for geeks and CS
The extracted string : best for geeks 
07

The original string is : Gfg is best for geeks and CS
The extracted string : best for geeks 
08
The original string is : Gfg is best for geeks and CS
The extracted string : best for geeks 
3
The original string is : Gfg is best for geeks and CS
The extracted string : best for geeks 
0
The original string is : Gfg is best for geeks and CS
The extracted string : best for geeks 
3
The original string is : Gfg is best for geeks and CS
The extracted string : best for geeks 
6
The original string is : Gfg is best for geeks and CS
The extracted string : best for geeks 
543

 

The original string is : Gfg is best for geeks and CS
The extracted string : best for geeks 
544

The original string is : Gfg is best for geeks and CS
The extracted string : best for geeks 
3______04
The original string is : Gfg is best for geeks and CS
The extracted string : best for geeks 
547
The original string is : Gfg is best for geeks and CS
The extracted string : best for geeks 
6
The original string is : Gfg is best for geeks and CS
The extracted string : best for geeks 
549

đầu ra

The original string is : Gfg is best for geeks and CS
The extracted string : best for geeks 

Sử dụng chỉ mục [] + cắt chuỗi để trích xuất chuỗi giữa hai chuỗi con

Tương tự như phương pháp trên, chỉ nhiệm vụ cắt được thực hiện bằng cách sử dụng cắt chuỗi để cung cấp một giải pháp nhỏ gọn hơn nhiều.  

Python3




The original string is : Gfg is best for geeks and CS
The extracted string : best for geeks 
54

The original string is : Gfg is best for geeks and CS
The extracted string : best for geeks 
55

The original string is : Gfg is best for geeks and CS
The extracted string : best for geeks 
552

 

The original string is : Gfg is best for geeks and CS
The extracted string : best for geeks 
57

The original string is : Gfg is best for geeks and CS
The extracted string : best for geeks 
58
The original string is : Gfg is best for geeks and CS
The extracted string : best for geeks 
0
The original string is : Gfg is best for geeks and CS
The extracted string : best for geeks 
1

 

The original string is : Gfg is best for geeks and CS
The extracted string : best for geeks 
2

The original string is : Gfg is best for geeks and CS
The extracted string : best for geeks 
3______04
The original string is : Gfg is best for geeks and CS
The extracted string : best for geeks 
5
The original string is : Gfg is best for geeks and CS
The extracted string : best for geeks 
6
The original string is : Gfg is best for geeks and CS
The extracted string : best for geeks 
7
The original string is : Gfg is best for geeks and CS
The extracted string : best for geeks 
8

 

The original string is : Gfg is best for geeks and CS
The extracted string : best for geeks 
9

The original string is : Gfg is best for geeks and CS
The extracted string : best for geeks 
0
The original string is : Gfg is best for geeks and CS
The extracted string : best for geeks 
0
The original string is : Gfg is best for geeks and CS
The extracted string : best for geeks 
2

The original string is : Gfg is best for geeks and CS
The extracted string : best for geeks 
3
The original string is : Gfg is best for geeks and CS
The extracted string : best for geeks 
0
The original string is : Gfg is best for geeks and CS
The extracted string : best for geeks 
5

 

The original string is : Gfg is best for geeks and CS
The extracted string : best for geeks 
6

The original string is : Gfg is best for geeks and CS
The extracted string : best for geeks 
7
The original string is : Gfg is best for geeks and CS
The extracted string : best for geeks 
0
The original string is : Gfg is best for geeks and CS
The extracted string : best for geeks 
9

The original string is : Gfg is best for geeks and CS
The extracted string : best for geeks 
0
The original string is : Gfg is best for geeks and CS
The extracted string : best for geeks 
0
The original string is : Gfg is best for geeks and CS
The extracted string : best for geeks 
2

 

The original string is : Gfg is best for geeks and CS
The extracted string : best for geeks 
578

The original string is : Gfg is best for geeks and CS
The extracted string : best for geeks 
579

The original string is : Gfg is best for geeks and CS
The extracted string : best for geeks 
3
The original string is : Gfg is best for geeks and CS
The extracted string : best for geeks 
0
The original string is : Gfg is best for geeks and CS
The extracted string : best for geeks 
582
The original string is : Gfg is best for geeks and CS
The extracted string : best for geeks 
6
The original string is : Gfg is best for geeks and CS
The extracted string : best for geeks 
03
The original string is : Gfg is best for geeks and CS
The extracted string : best for geeks 
04
The original string is : Gfg is best for geeks and CS
The extracted string : best for geeks 
6
The original string is : Gfg is best for geeks and CS
The extracted string : best for geeks 
06
The original string is : Gfg is best for geeks and CS
The extracted string : best for geeks 
588

 

The original string is : Gfg is best for geeks and CS
The extracted string : best for geeks 
544

The original string is : Gfg is best for geeks and CS
The extracted string : best for geeks 
3______04
The original string is : Gfg is best for geeks and CS
The extracted string : best for geeks 
547
The original string is : Gfg is best for geeks and CS
The extracted string : best for geeks 
6
The original string is : Gfg is best for geeks and CS
The extracted string : best for geeks 
549

đầu ra

The original string is : Gfg is best for geeks and CS
The extracted string : best for geeks 

Sử dụng find[]+ cắt chuỗi để trích xuất chuỗi giữa hai chuỗi con

phương thức find[] trả về vị trí của chuỗi được truyền dưới dạng đối số hoặc nếu không thì trả về -1. Cuối cùng cắt chuỗi với các vị trí

Python3




The original string is : Gfg is best for geeks and CS
The extracted string : best for geeks 
54

The original string is : Gfg is best for geeks and CS
The extracted string : best for geeks 
55

The original string is : Gfg is best for geeks and CS
The extracted string : best for geeks 
07

 

The original string is : Gfg is best for geeks and CS
The extracted string : best for geeks 
57

The original string is : Gfg is best for geeks and CS
The extracted string : best for geeks 
58
The original string is : Gfg is best for geeks and CS
The extracted string : best for geeks 
0
The original string is : Gfg is best for geeks and CS
The extracted string : best for geeks 
1

 

The original string is : Gfg is best for geeks and CS
The extracted string : best for geeks 
2

The original string is : Gfg is best for geeks and CS
The extracted string : best for geeks 
3______04
The original string is : Gfg is best for geeks and CS
The extracted string : best for geeks 
5
The original string is : Gfg is best for geeks and CS
The extracted string : best for geeks 
6
The original string is : Gfg is best for geeks and CS
The extracted string : best for geeks 
7
The original string is : Gfg is best for geeks and CS
The extracted string : best for geeks 
8

 

The original string is : Gfg is best for geeks and CS
The extracted string : best for geeks 
9

The original string is : Gfg is best for geeks and CS
The extracted string : best for geeks 
0
The original string is : Gfg is best for geeks and CS
The extracted string : best for geeks 
0
The original string is : Gfg is best for geeks and CS
The extracted string : best for geeks 
2

The original string is : Gfg is best for geeks and CS
The extracted string : best for geeks 
3
The original string is : Gfg is best for geeks and CS
The extracted string : best for geeks 
0
The original string is : Gfg is best for geeks and CS
The extracted string : best for geeks 
5

 

The original string is : Gfg is best for geeks and CS
The extracted string : best for geeks 
6

The original string is : Gfg is best for geeks and CS
The extracted string : best for geeks 
7
The original string is : Gfg is best for geeks and CS
The extracted string : best for geeks 
0
The original string is : Gfg is best for geeks and CS
The extracted string : best for geeks 
29

The original string is : Gfg is best for geeks and CS
The extracted string : best for geeks 
0
The original string is : Gfg is best for geeks and CS
The extracted string : best for geeks 
0
The original string is : Gfg is best for geeks and CS
The extracted string : best for geeks 
32

 

The original string is : Gfg is best for geeks and CS
The extracted string : best for geeks 
578

The original string is : Gfg is best for geeks and CS
The extracted string : best for geeks 
579

The original string is : Gfg is best for geeks and CS
The extracted string : best for geeks 
3
The original string is : Gfg is best for geeks and CS
The extracted string : best for geeks 
0
The original string is : Gfg is best for geeks and CS
The extracted string : best for geeks 
582
The original string is : Gfg is best for geeks and CS
The extracted string : best for geeks 
6
The original string is : Gfg is best for geeks and CS
The extracted string : best for geeks 
03
The original string is : Gfg is best for geeks and CS
The extracted string : best for geeks 
04
The original string is : Gfg is best for geeks and CS
The extracted string : best for geeks 
6
The original string is : Gfg is best for geeks and CS
The extracted string : best for geeks 
06
The original string is : Gfg is best for geeks and CS
The extracted string : best for geeks 
588

 

The original string is : Gfg is best for geeks and CS
The extracted string : best for geeks 
544

The original string is : Gfg is best for geeks and CS
The extracted string : best for geeks 
3______04
The original string is : Gfg is best for geeks and CS
The extracted string : best for geeks 
547
The original string is : Gfg is best for geeks and CS
The extracted string : best for geeks 
6
The original string is : Gfg is best for geeks and CS
The extracted string : best for geeks 
549

đầu ra

The original string is : Gfg is best for geeks and CS
The extracted string : best for geeks 

Sử dụng replace[] và split[] để trích xuất chuỗi giữa hai chuỗi con

Ở đây chúng tôi đang sử dụng thay thế và tách chuỗi gốc cho đến khi chúng tôi nhận được các chuỗi con mong muốn ở góc và sau đó giải nén nó

Python3




The original string is : Gfg is best for geeks and CS
The extracted string : best for geeks 
54

The original string is : Gfg is best for geeks and CS
The extracted string : best for geeks 
55

 

The original string is : Gfg is best for geeks and CS
The extracted string : best for geeks 
57

The original string is : Gfg is best for geeks and CS
The extracted string : best for geeks 
58
The original string is : Gfg is best for geeks and CS
The extracted string : best for geeks 
0
The original string is : Gfg is best for geeks and CS
The extracted string : best for geeks 
1

 

The original string is : Gfg is best for geeks and CS
The extracted string : best for geeks 
2

The original string is : Gfg is best for geeks and CS
The extracted string : best for geeks 
3______04
The original string is : Gfg is best for geeks and CS
The extracted string : best for geeks 
5
The original string is : Gfg is best for geeks and CS
The extracted string : best for geeks 
6
The original string is : Gfg is best for geeks and CS
The extracted string : best for geeks 
7
The original string is : Gfg is best for geeks and CS
The extracted string : best for geeks 
8

 

The original string is : Gfg is best for geeks and CS
The extracted string : best for geeks 
9

The original string is : Gfg is best for geeks and CS
The extracted string : best for geeks 
0
The original string is : Gfg is best for geeks and CS
The extracted string : best for geeks 
0
The original string is : Gfg is best for geeks and CS
The extracted string : best for geeks 
2

The original string is : Gfg is best for geeks and CS
The extracted string : best for geeks 
3
The original string is : Gfg is best for geeks and CS
The extracted string : best for geeks 
0
The original string is : Gfg is best for geeks and CS
The extracted string : best for geeks 
5

 

The original string is : Gfg is best for geeks and CS
The extracted string : best for geeks 
58
The original string is : Gfg is best for geeks and CS
The extracted string : best for geeks 
0
The original string is : Gfg is best for geeks and CS
The extracted string : best for geeks 
72
The original string is : Gfg is best for geeks and CS
The extracted string : best for geeks 
73
The original string is : Gfg is best for geeks and CS
The extracted string : best for geeks 
74

The original string is : Gfg is best for geeks and CS
The extracted string : best for geeks 
58
The original string is : Gfg is best for geeks and CS
The extracted string : best for geeks 
0
The original string is : Gfg is best for geeks and CS
The extracted string : best for geeks 
77
The original string is : Gfg is best for geeks and CS
The extracted string : best for geeks 
73
The original string is : Gfg is best for geeks and CS
The extracted string : best for geeks 
74

The original string is : Gfg is best for geeks and CS
The extracted string : best for geeks 
80
The original string is : Gfg is best for geeks and CS
The extracted string : best for geeks 
0____082
The original string is : Gfg is best for geeks and CS
The extracted string : best for geeks 
73
The original string is : Gfg is best for geeks and CS
The extracted string : best for geeks 
74

_______03____00____087

The original string is : Gfg is best for geeks and CS
The extracted string : best for geeks 
06
The original string is : Gfg is best for geeks and CS
The extracted string : best for geeks 
89

 

The original string is : Gfg is best for geeks and CS
The extracted string : best for geeks 
544

The original string is : Gfg is best for geeks and CS
The extracted string : best for geeks 
3______04
The original string is : Gfg is best for geeks and CS
The extracted string : best for geeks 
547
The original string is : Gfg is best for geeks and CS
The extracted string : best for geeks 
6
The original string is : Gfg is best for geeks and CS
The extracted string : best for geeks 
549

đầu ra

The original string is : Gfg is best for geeks and CS
The extracted string : best for geeks 

Sử dụng regex để trích xuất chuỗi giữa hai chuỗi con

Ở đây chúng tôi đang sử dụng một thư viện tích hợp sẵn tôi. e. chúng tôi đang sử dụng biểu thức chính quy để trích xuất chuỗi giữa hai chuỗi con

Chủ Đề