【发布时间】:2018-12-12 19:50:04
【问题描述】:
我想计算\n 在字符串(Student Copy)\nfor\nspecial school\n...Shaping the Future\n408,) 中出现在短语Shaping the Future 之前的次数。有没有不拆分字符串的方法?
这种情况下的输出应该是 3
【问题讨论】:
标签: python python-3.x
我想计算\n 在字符串(Student Copy)\nfor\nspecial school\n...Shaping the Future\n408,) 中出现在短语Shaping the Future 之前的次数。有没有不拆分字符串的方法?
这种情况下的输出应该是 3
【问题讨论】:
标签: python python-3.x
您可以将字符串切分到您感兴趣的子字符串,然后使用count
s = """(Student Copy)\nfor\nspecial school\n...Shaping the Future\n408,)"""
s[:s.index("Shaping the Future")].count('\n')
【讨论】: