📜  Python - 在两个子字符串之间提取字符串

📅  最后修改于: 2022-05-13 01:55:01.252000             🧑  作者: Mango

Python - 在两个子字符串之间提取字符串

给定一个字符串和两个子字符串,编写一个Python程序来提取找到的两个子字符串之间的字符串。

方法 #1:使用index() +循环

在这里,我们使用 index() 获取两个子字符串的索引,然后使用循环在索引内迭代以找到它们之间所需的字符串。

Python3
# Python3 code to demonstrate working
# of Extract string between 2 substrings
# Using loop + index()
  
# initializing string
test_str = "Gfg is best for geeks and CS"
  
# printing original string
print("The original string is : " + str(test_str))
  
# initializing substrings
sub1 = "is"
sub2 = "and"
  
# getting index of substrings
idx1 = test_str.index(sub1)
idx2 = test_str.index(sub2)
  
res = ''
# getting elements in between
for idx in range(idx1 + len(sub1) + 1, idx2):
    res = res + test_str[idx]
  
# printing result
print("The extracted string : " + res)


Python3
# Python3 code to demonstrate working 
# of Extract string between 2 substrings
# Using index() + string slicing
  
# initializing string
test_str = "Gfg is best for geeks and CS"
  
# printing original string
print("The original string is : " + str(test_str))
  
# initializing substrings
sub1 = "is"
sub2 = "and"
  
# getting index of substrings
idx1 = test_str.index(sub1)
idx2 = test_str.index(sub2)
  
# length of substring 1 is added to
# get string from next character
res = test_str[idx1 + len(sub1) + 1: idx2]
  
# printing result
print("The extracted string : " + res)


输出:

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

方法 #2:使用index() +字符串切片

与上述方法类似,只是使用字符串切片来执行切片任务,以提供更紧凑的解决方案。

蟒蛇3

# Python3 code to demonstrate working 
# of Extract string between 2 substrings
# Using index() + string slicing
  
# initializing string
test_str = "Gfg is best for geeks and CS"
  
# printing original string
print("The original string is : " + str(test_str))
  
# initializing substrings
sub1 = "is"
sub2 = "and"
  
# getting index of substrings
idx1 = test_str.index(sub1)
idx2 = test_str.index(sub2)
  
# length of substring 1 is added to
# get string from next character
res = test_str[idx1 + len(sub1) + 1: idx2]
  
# printing result
print("The extracted string : " + res)

输出:

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