Bagaimana Anda mengekstrak bagian dari string dengan python?

Diberi string dan dua substring, tulis program Python untuk mengekstrak string di antara dua substring yang ditemukan.  

Contoh

Memasukkan. test_str = “Gfg terbaik untuk Geeks dan CS”, sub1 = “adalah”, sub2 = “dan”

Keluaran. terbaik untuk geek

Penjelasan. terbaik untuk Geeks adalah antara is dan 'and'

Memasukkan. test_str = “Gfg terbaik untuk Geeks dan CS”, sub1 = “untuk”, sub2 = “dan”

Keluaran. geek

Penjelasan. geeks adalah antara untuk dan 'dan'

Menggunakan index() + loop untuk mengekstrak string antara dua substring

Dalam hal ini, kita mendapatkan indeks dari kedua substring menggunakan index(), lalu sebuah loop digunakan untuk mengulang dalam indeks untuk menemukan string yang diperlukan di antara substring tersebut

Python3




# Python3 code to demonstrate working

# of Extract string between 2 substrings

# Using loop + index()

 

# initializing string

test_str

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
The original string is : Gfg is best for geeks and CS
The extracted string : best for geeks 
4
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 
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 
6 ________30______3
The original string is : Gfg is best for geeks and CS
The extracted string :  best for geeks 
4
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 
6________30______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 
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 # Python3 code to demonstrate working3

 

# Python3 code to demonstrate working_4

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 
4# Python3 code to demonstrate working7
The original string is : Gfg is best for geeks and CS
The extracted string : best for geeks 
6 # Python3 code to demonstrate working9

Keluaran

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

Menggunakan index() + string slicing untuk mengekstrak string antara dua substring

Mirip dengan metode di atas, hanya tugas pemotongan dilakukan menggunakan pemotongan string untuk memberikan solusi yang lebih ringkas.  

Python3




# Python3 code to demonstrate working

# of Extract string between 2 substrings

# of Extract string between 2 substrings_2

 

# initializing string

test_str

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
The original string is : Gfg is best for geeks and CS
The extracted string : best for geeks 
4
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

 

# initializing string_8

# initializing string_9

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 test_str2
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 
3
The original string is : Gfg is best for geeks and CS
The extracted string :  best for geeks 
4
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 
6test_str8

 

# Python3 code to demonstrate working_4

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 
4# Python3 code to demonstrate working7
The original string is : Gfg is best for geeks and CS
The extracted string : best for geeks 
6 # Python3 code to demonstrate working9

Keluaran

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

Menggunakan find()+ string slicing untuk mengekstraksi string di antara dua substring

metode find() mengembalikan posisi string yang diteruskan sebagai argumen atau mengembalikan -1. Akhirnya potong string dengan posisi

Python3




# Python3 code to demonstrate working

# of Extract string between 2 substrings

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

 

# initializing string

test_str

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
The original string is : Gfg is best for geeks and CS
The extracted string : best for geeks 
4
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

 

# initializing string_8

# initializing string_9

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 test_str2
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 
3
The original string is : Gfg is best for geeks and CS
The extracted string :  best for geeks 
4
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 
6test_str8

 

# Python3 code to demonstrate working_4

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 
4# Python3 code to demonstrate working7
The original string is : Gfg is best for geeks and CS
The extracted string : best for geeks 
6 # Python3 code to demonstrate working9

Keluaran

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

Menggunakan replace() dan split() untuk mengekstrak string antara dua substring

Di sini kita menggunakan penggantian dan pemisahan string asli sampai kita mendapatkan substring yang diinginkan di pojok dan kemudian mengekstraknya

Python3




# Python3 code to demonstrate working

# of Extract string between 2 substrings

 

# initializing string

test_str

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
The original string is : Gfg is best for geeks and CS
The extracted string : best for geeks 
4
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

 

test_str

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

test_str

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
The original string is : Gfg is best for geeks and CS
The extracted string : best for geeks 
82
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 
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 
87
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 
89

 

# Python3 code to demonstrate working_4

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 
4# Python3 code to demonstrate working7
The original string is : Gfg is best for geeks and CS
The extracted string : best for geeks 
6 # Python3 code to demonstrate working9

Keluaran

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

Menggunakan regex untuk mengekstrak string antara dua substring

Di sini kita menggunakan pustaka bawaan i. e. kami menggunakan regex untuk mengekstrak string antara dua substring

Bagaimana Anda mengekstrak bagian tertentu dari sebuah string?

Metode substr() mengekstrak bagian dari string. Metode substr() dimulai pada posisi tertentu, dan mengembalikan sejumlah karakter tertentu. Metode substr() tidak mengubah string asli. Untuk mengekstrak karakter dari akhir string, gunakan posisi awal negatif.

Bagaimana cara mengekstrak kata tertentu dari string dengan Python?

Metode #1. Menggunakan split() .