检测具有特殊字符的字符串之间的子字符串

时间:2021-02-28 11:29:59

标签: python string special-characters

我有 6 种类型的字符串(在对它们执行 strip() 之后):

string_1= 'This is a \'working draft\' sequence. It currently consists of 10 contigs. Gaps between the contigsare represented as runs of N. The order of the piecesis believed to be correct as given, however the sizesof the gaps between them are based on estimates that haveprovided by the submittor.This sequence will be replacedby the finished sequence as soon as it is available andthe accession number will be preserved.\n???UPDATE FROM "This record contains 83 individual sequencing reads that have not been assembled intocontigs. Runs of N are used to separate the readsand the order in which they appear is completelyarbitrary. Low-pass sequence sampling is useful foridentifying clones that may be gene-rich and allowsoverlap relationships among clones to be deduced.However, it should not be assumed that this clonewill be sequenced to completion. In the event thatthe record is updated, the accession number willbe preserved."???'

string_2= '???INSERT information???\n\nPlasmid; n/a; 100% of reads'

string_3= '???INSERT information???\n\ngap of      100 bp'

string_x= "This is a 'working draft' sequence. It currently consists of 10 contigs. Gaps between the contigsare represented as runs of N. The order of the piecesis believed to be correct as given, however the sizesof the gaps between them are based on estimates that haveprovided by the submittor.This sequence will be replacedby the finished sequence as soon as it is available andthe accession number will be preserved."

string_y= 'Plasmid; n/a; 100% of reads'

string_z= 'gap of      100 bp'

我正在努力做到:

if string_x in string_1:
    print("true")

if string_y in string_2:
    print("true")

if string_z in string_3:
    print("true")

然而,即使我去掉所有的字符串,这也不起作用。

我应该怎么做?

2 个答案:

答案 0 :(得分:1)

您可以尝试将字符串调整为一种格式

string_1 = ''.join([x for x in string_1  if x.isalpha() or x == ' '])
string_x = ''.join([x for x in string_x  if x.isalpha() or x == ' '])

答案 1 :(得分:1)

首先您需要了解 str.strip() 方法的作用。它返回删除了前导和尾随字符的字符串副本。

语法:str. strip([chars])

示例 1

>>> '   spacious   '.strip()
'spacious'
>>> "AABAA".strip("A")
'B'
>>> "ABBA".strip("AB")
''
>>> "ABCABBA".strip("AB")
'C'

示例 2

>>> 'www.example.com'.strip('cmowz.') # this example extracts web address
'example'

有关更多信息,您可以阅读Official Documentation.