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,172
  • 2
  • 22
  • 37

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
  • 57,686
  • 15
  • 58
  • 82