如何搜索變量並查找大於20的字/字符串?查找長度大於(x)的字符串
var = '''this is my example and I want
no find the following
string jakjajsiwjsjwijaksjdfjaiwjalskdjfiajwlajfklajsdfwi
from this variable'''
由於使用split
,檢查每個單詞的len
如何搜索變量並查找大於20的字/字符串?查找長度大於(x)的字符串
var = '''this is my example and I want
no find the following
string jakjajsiwjsjwijaksjdfjaiwjalskdjfiajwlajfklajsdfwi
from this variable'''
由於使用split
,檢查每個單詞的len
使用列表理解:
print [x for x in var.split() if len(x) > 20]
通過迭代的單詞列表。您可以使用list comprehension。
long_words = [x for x in var.split() if len(x) > 20]
print(long_words)
var_split = var.split(' ')
long_word = ''
for word in var_split:
if len(word) > 20:
long_word = word
啊,謝謝! –