2014-07-17 54 views
0

在Python中,我想讀取輸入,然後只在某個點後打印。我會爲它工作像這樣在python中打印某個單詞後

humaninput = raw_input("Please enter:") 
    breakdown = humaninput.split() 
    say = "say" 
    if say in breakdown: 
     print (all words after say) 

我除了最後一部分

+0

這裏需要澄清一點。這裏所有的答案都是建議在首次發生「say」之後進行分裂的方法。如果你想改變一些其他的「說」,你可以使用'enumerate()'函數遍歷列表並根據你的需要修改輸出。 – AnotherCodingEnthusiast

回答

0

由於您將所有條目轉換爲列表,您可以找到「say」的第一個實例,然後創建一個包含所有條目的新列表。

humaninput = "This is me typing a whole bunch of say things with words after it" 
breakdown = humaninput.split() 
say = "say" 
if say in breakdown: 
    split = breakdown.index(say) 
    after = breakdown[split+1:] 
    print(after) 
0

這是很容易的,如果你只是使用字符串split()做的一切。

if say in humaninput: 
    saysplit = humaninput.split(say,1) 
    print saysplit[1] 

它適用於整個字符串,不只是單個字符或什麼都沒有(它默認爲空格)。如果你有一個清單,另一個答案是正確的。

+1

我會將maxsplit設置爲1,否則如果字符串中有多個「say」,你會得到奇怪的輸出。 –

+0

@PadraicCunningham好點,謝謝。編輯。 – TheSoundDefense

0

這是一個很好的選擇,不使用split

string = "string blah say foo bar" 
say = "say" 
after = string[string.index(say) + len(say):] # +1 if you're worried about spaces 
print(after) 

>> foo bar 

如果有多個「說」的實例,它將採取第一個。

相關問題