我想数一数\n在字符串(Student Copy)\nfor\nspecial school\n...Shaping the Future\n408,)中出现在短语Shaping the Future之前的次数。有没有办法做到这一点而不分裂字符串?
在这种情况下,输出应为3。
发布于 2018-07-04 11:25:09
可以将字符串分割到感兴趣的子字符串,然后使用count
s = """(Student Copy)\nfor\nspecial school\n...Shaping the Future\n408,)"""
s[:s.index("Shaping the Future")].count('\n')https://stackoverflow.com/questions/51172509
复制相似问题