2016-11-22 21 views
0
string='I love #Snorlax he is a beast #op#letsgo' 
for item in string.split(): 
    if item.startswith('#'): 
     print(item) 

我的代碼的問題是,輸出我得到的是:Occurence串的Python

#Snorlax 
#op#letsgo 

輸出應該

#Snorlax 
#op 
+0

不,這是*不是您發佈的代碼的輸出。你可能忘了分割'string'。 –

回答

2

試試這個:

string='I love #Snorlax he is a beast #op#letsgo' 
for item in string.split(): 
    if item.startswith("#"): 
     print("#" + item.split("#")[1]) 
+0

,仍然給我'#Snorlax #op#letsgo' – rassar

+0

哎呀,沒關係! –