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 geeks54
The original string is : Gfg is best for geeks and CS The extracted string : best for geeks55
The original string is : Gfg is best for geeks and CS The extracted string : best for geeks56
The original string is : Gfg is best for geeks and CS The extracted string : best for geeks57
The original string is : Gfg is best for geeks and CS The extracted string : best for geeks58
The original string is : Gfg is best for geeks and CS The extracted string : best for geeks0
The original string is : Gfg is best for geeks and CS The extracted string : best for geeks1
The original string is : Gfg is best for geeks and CS The extracted string : best for geeks2
The original string is : Gfg is best for geeks and CS The extracted string : best for geeks3______04
The original string is : Gfg is best for geeks and CS The extracted string : best for geeks5
The original string is : Gfg is best for geeks and CS The extracted string : best for geeks6
The original string is : Gfg is best for geeks and CS The extracted string : best for geeks7
The original string is : Gfg is best for geeks and CS The extracted string : best for geeks8
The original string is : Gfg is best for geeks and CS The extracted string : best for geeks9
The original string is : Gfg is best for geeks and CS The extracted string : best for geeks0
The original string is : Gfg is best for geeks and CS The extracted string : best for geeks0
The original string is : Gfg is best for geeks and CS The extracted string : best for geeks2
The original string is : Gfg is best for geeks and CS The extracted string : best for geeks3
The original string is : Gfg is best for geeks and CS The extracted string : best for geeks0
The original string is : Gfg is best for geeks and CS The extracted string : best for geeks5
The original string is : Gfg is best for geeks and CS The extracted string : best for geeks6
The original string is : Gfg is best for geeks and CS The extracted string : best for geeks7
The original string is : Gfg is best for geeks and CS The extracted string : best for geeks0
The original string is : Gfg is best for geeks and CS The extracted string : best for geeks9
The original string is : Gfg is best for geeks and CS The extracted string : best for geeks0
The original string is : Gfg is best for geeks and CS The extracted string : best for geeks0
The original string is : Gfg is best for geeks and CS The extracted string : best for geeks2
The original string is : Gfg is best for geeks and CS The extracted string : best for geeks3
The original string is : Gfg is best for geeks and CS The extracted string : best for geeks0
The original string is : Gfg is best for geeks and CS The extracted string : best for geeks5
The original string is : Gfg is best for geeks and CS The extracted string : best for geeks6
The original string is : Gfg is best for geeks and CS The extracted string : best for geeks7
The original string is : Gfg is best for geeks and CS The extracted string : best for geeks8
The original string is : Gfg is best for geeks and CS The extracted string : best for geeks9
The original string is : Gfg is best for geeks and CS The extracted string : best for geeks00
The original string is : Gfg is best for geeks and CS The extracted string : best for geeks01
The original string is : Gfg is best for geeks and CS The extracted string : best for geeks6
The original string is : Gfg is best for geeks and CS The extracted string : best for geeks03
The original string is : Gfg is best for geeks and CS The extracted string : best for geeks04
The original string is : Gfg is best for geeks and CS The extracted string : best for geeks6
The original string is : Gfg is best for geeks and CS The extracted string : best for geeks06
The original string is : Gfg is best for geeks and CS The extracted string : best for geeks07
The original string is : Gfg is best for geeks and CS The extracted string : best for geeks08
The original string is : Gfg is best for geeks and CS The extracted string : best for geeks3
The original string is : Gfg is best for geeks and CS The extracted string : best for geeks0
The original string is : Gfg is best for geeks and CS The extracted string : best for geeks3
The original string is : Gfg is best for geeks and CS The extracted string : best for geeks6
The original string is : Gfg is best for geeks and CS The extracted string : best for geeks543
The original string is : Gfg is best for geeks and CS The extracted string : best for geeks544
The original string is : Gfg is best for geeks and CS The extracted string : best for geeks3______04
The original string is : Gfg is best for geeks and CS The extracted string : best for geeks547
The original string is : Gfg is best for geeks and CS The extracted string : best for geeks6
The original string is : Gfg is best for geeks and CS The extracted string : best for geeks549
đầ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 geeks54
The original string is : Gfg is best for geeks and CS The extracted string : best for geeks55
The original string is : Gfg is best for geeks and CS The extracted string : best for geeks552
The original string is : Gfg is best for geeks and CS The extracted string : best for geeks57
The original string is : Gfg is best for geeks and CS The extracted string : best for geeks58
The original string is : Gfg is best for geeks and CS The extracted string : best for geeks0
The original string is : Gfg is best for geeks and CS The extracted string : best for geeks1
The original string is : Gfg is best for geeks and CS The extracted string : best for geeks2
The original string is : Gfg is best for geeks and CS The extracted string : best for geeks3______04
The original string is : Gfg is best for geeks and CS The extracted string : best for geeks5
The original string is : Gfg is best for geeks and CS The extracted string : best for geeks6
The original string is : Gfg is best for geeks and CS The extracted string : best for geeks7
The original string is : Gfg is best for geeks and CS The extracted string : best for geeks8
The original string is : Gfg is best for geeks and CS The extracted string : best for geeks9
The original string is : Gfg is best for geeks and CS The extracted string : best for geeks0
The original string is : Gfg is best for geeks and CS The extracted string : best for geeks0
The original string is : Gfg is best for geeks and CS The extracted string : best for geeks2
The original string is : Gfg is best for geeks and CS The extracted string : best for geeks3
The original string is : Gfg is best for geeks and CS The extracted string : best for geeks0
The original string is : Gfg is best for geeks and CS The extracted string : best for geeks5
The original string is : Gfg is best for geeks and CS The extracted string : best for geeks6
The original string is : Gfg is best for geeks and CS The extracted string : best for geeks7
The original string is : Gfg is best for geeks and CS The extracted string : best for geeks0
The original string is : Gfg is best for geeks and CS The extracted string : best for geeks9
The original string is : Gfg is best for geeks and CS The extracted string : best for geeks0
The original string is : Gfg is best for geeks and CS The extracted string : best for geeks0
The original string is : Gfg is best for geeks and CS The extracted string : best for geeks2
The original string is : Gfg is best for geeks and CS The extracted string : best for geeks578
The original string is : Gfg is best for geeks and CS The extracted string : best for geeks579
The original string is : Gfg is best for geeks and CS The extracted string : best for geeks3
The original string is : Gfg is best for geeks and CS The extracted string : best for geeks0
The original string is : Gfg is best for geeks and CS The extracted string : best for geeks582
The original string is : Gfg is best for geeks and CS The extracted string : best for geeks6
The original string is : Gfg is best for geeks and CS The extracted string : best for geeks03
The original string is : Gfg is best for geeks and CS The extracted string : best for geeks04
The original string is : Gfg is best for geeks and CS The extracted string : best for geeks6
The original string is : Gfg is best for geeks and CS The extracted string : best for geeks06
The original string is : Gfg is best for geeks and CS The extracted string : best for geeks588
The original string is : Gfg is best for geeks and CS The extracted string : best for geeks544
The original string is : Gfg is best for geeks and CS The extracted string : best for geeks3______04
The original string is : Gfg is best for geeks and CS The extracted string : best for geeks547
The original string is : Gfg is best for geeks and CS The extracted string : best for geeks6
The original string is : Gfg is best for geeks and CS The extracted string : best for geeks549
đầ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 geeks54
The original string is : Gfg is best for geeks and CS The extracted string : best for geeks55
The original string is : Gfg is best for geeks and CS The extracted string : best for geeks07
The original string is : Gfg is best for geeks and CS The extracted string : best for geeks57
The original string is : Gfg is best for geeks and CS The extracted string : best for geeks58
The original string is : Gfg is best for geeks and CS The extracted string : best for geeks0
The original string is : Gfg is best for geeks and CS The extracted string : best for geeks1
The original string is : Gfg is best for geeks and CS The extracted string : best for geeks2
The original string is : Gfg is best for geeks and CS The extracted string : best for geeks3______04
The original string is : Gfg is best for geeks and CS The extracted string : best for geeks5
The original string is : Gfg is best for geeks and CS The extracted string : best for geeks6
The original string is : Gfg is best for geeks and CS The extracted string : best for geeks7
The original string is : Gfg is best for geeks and CS The extracted string : best for geeks8
The original string is : Gfg is best for geeks and CS The extracted string : best for geeks9
The original string is : Gfg is best for geeks and CS The extracted string : best for geeks0
The original string is : Gfg is best for geeks and CS The extracted string : best for geeks0
The original string is : Gfg is best for geeks and CS The extracted string : best for geeks2
The original string is : Gfg is best for geeks and CS The extracted string : best for geeks3
The original string is : Gfg is best for geeks and CS The extracted string : best for geeks0
The original string is : Gfg is best for geeks and CS The extracted string : best for geeks5
The original string is : Gfg is best for geeks and CS The extracted string : best for geeks6
The original string is : Gfg is best for geeks and CS The extracted string : best for geeks7
The original string is : Gfg is best for geeks and CS The extracted string : best for geeks0
The original string is : Gfg is best for geeks and CS The extracted string : best for geeks29
The original string is : Gfg is best for geeks and CS The extracted string : best for geeks0
The original string is : Gfg is best for geeks and CS The extracted string : best for geeks0
The original string is : Gfg is best for geeks and CS The extracted string : best for geeks32
The original string is : Gfg is best for geeks and CS The extracted string : best for geeks578
The original string is : Gfg is best for geeks and CS The extracted string : best for geeks579
The original string is : Gfg is best for geeks and CS The extracted string : best for geeks3
The original string is : Gfg is best for geeks and CS The extracted string : best for geeks0
The original string is : Gfg is best for geeks and CS The extracted string : best for geeks582
The original string is : Gfg is best for geeks and CS The extracted string : best for geeks6
The original string is : Gfg is best for geeks and CS The extracted string : best for geeks03
The original string is : Gfg is best for geeks and CS The extracted string : best for geeks04
The original string is : Gfg is best for geeks and CS The extracted string : best for geeks6
The original string is : Gfg is best for geeks and CS The extracted string : best for geeks06
The original string is : Gfg is best for geeks and CS The extracted string : best for geeks588
The original string is : Gfg is best for geeks and CS The extracted string : best for geeks544
The original string is : Gfg is best for geeks and CS The extracted string : best for geeks3______04
The original string is : Gfg is best for geeks and CS The extracted string : best for geeks547
The original string is : Gfg is best for geeks and CS The extracted string : best for geeks6
The original string is : Gfg is best for geeks and CS The extracted string : best for geeks549
đầ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 geeks54
The original string is : Gfg is best for geeks and CS The extracted string : best for geeks55
The original string is : Gfg is best for geeks and CS The extracted string : best for geeks57
The original string is : Gfg is best for geeks and CS The extracted string : best for geeks58
The original string is : Gfg is best for geeks and CS The extracted string : best for geeks0
The original string is : Gfg is best for geeks and CS The extracted string : best for geeks1
The original string is : Gfg is best for geeks and CS The extracted string : best for geeks2
The original string is : Gfg is best for geeks and CS The extracted string : best for geeks3______04
The original string is : Gfg is best for geeks and CS The extracted string : best for geeks5
The original string is : Gfg is best for geeks and CS The extracted string : best for geeks6
The original string is : Gfg is best for geeks and CS The extracted string : best for geeks7
The original string is : Gfg is best for geeks and CS The extracted string : best for geeks8
The original string is : Gfg is best for geeks and CS The extracted string : best for geeks9
The original string is : Gfg is best for geeks and CS The extracted string : best for geeks0
The original string is : Gfg is best for geeks and CS The extracted string : best for geeks0
The original string is : Gfg is best for geeks and CS The extracted string : best for geeks2
The original string is : Gfg is best for geeks and CS The extracted string : best for geeks3
The original string is : Gfg is best for geeks and CS The extracted string : best for geeks0
The original string is : Gfg is best for geeks and CS The extracted string : best for geeks5
The original string is : Gfg is best for geeks and CS The extracted string : best for geeks58
The original string is : Gfg is best for geeks and CS The extracted string : best for geeks0
The original string is : Gfg is best for geeks and CS The extracted string : best for geeks72
The original string is : Gfg is best for geeks and CS The extracted string : best for geeks73
The original string is : Gfg is best for geeks and CS The extracted string : best for geeks74
The original string is : Gfg is best for geeks and CS The extracted string : best for geeks58
The original string is : Gfg is best for geeks and CS The extracted string : best for geeks0
The original string is : Gfg is best for geeks and CS The extracted string : best for geeks77
The original string is : Gfg is best for geeks and CS The extracted string : best for geeks73
The original string is : Gfg is best for geeks and CS The extracted string : best for geeks74
The original string is : Gfg is best for geeks and CS The extracted string : best for geeks80
The original string is : Gfg is best for geeks and CS The extracted string : best for geeks0____082
The original string is : Gfg is best for geeks and CS The extracted string : best for geeks73
The original string is : Gfg is best for geeks and CS The extracted string : best for geeks74
_______03____00____087
The original string is : Gfg is best for geeks and CS The extracted string : best for geeks06
The original string is : Gfg is best for geeks and CS The extracted string : best for geeks89
The original string is : Gfg is best for geeks and CS The extracted string : best for geeks544
The original string is : Gfg is best for geeks and CS The extracted string : best for geeks3______04
The original string is : Gfg is best for geeks and CS The extracted string : best for geeks547
The original string is : Gfg is best for geeks and CS The extracted string : best for geeks6
The original string is : Gfg is best for geeks and CS The extracted string : best for geeks549
đầ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