繁体   English   中英

在python中的某个单词后打印

[英]print after a certain word in python

在Python中,我想读取输入,然后仅在特定点之后打印。 它像这样工作

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

除了最后一部分,我什么都有

这是一个不使用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”实例,则将采用第一个实例。

如果只使用字符串,使用split()可以很容易地做到这一点。

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

它适用于整个字符串,而不仅仅是单个字符或根本不起作用(默认为空格)。 如果您有列表,则其他答案是正确的。

由于将所有条目转换为列表,因此可以找到“ 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)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM