2014-07-17 4 views
0

파이썬에서는 입력을 읽은 다음 특정 지점 이후에만 인쇄하고 싶습니다. 나는 처럼 내가 마지막 부분파이썬에서 특정 단어 뒤에 인쇄

+0

여기에 작은 설명이 필요합니다. 여기에있는 모든 대답은 "say"의 첫 번째 발생 후 분할하는 방법입니다. "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()과는 꽤 쉽게 제외한 모든 것을 가지고이

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

처럼 작동하는 것이기 때문.

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 

"say"의 인스턴스가 여러 개인 경우 첫 번째가됩니다.

관련 문제