0

I want to count the number of times \n appears in string (Student Copy)\nfor\nspecial school\n...Shaping the Future\n408,) before the phrase Shaping the Future. Is there a way to do it without splitting the string?

Output in this case should be 3

Sujay DSa
  • 1,084
  • 2
  • 19
  • 36

1 Answers1

0

You can slice the string up until your substring of interest, and then use count

s = """(Student Copy)\nfor\nspecial school\n...Shaping the Future\n408,)""" 
s[:s.index("Shaping the Future")].count('\n')
rafaelc
  • 52,436
  • 15
  • 51
  • 78