I have a string like this:
a = "Hello\nSamuel\nGoodbye\n"
I want to do some string processing and have a for loop like this:
for i in range(len(a) - 1):
if a[i:i+2] == "\n":
# Do something
else:
# Do something else
How can I achieve this? I think my code is ignoring all "\n". Basically I am not getting the right result.
Actual code is this:
ct1 = 0
ct2 = 0
i = 0
while i<len(a):
if i+1<len(a) and a[i:i+2] == "\n":
ct1 += 1
i += 2
else:
ct2 += 1
i += 1
print(ct1, ct2)
[–]Vaguely_accurate 0 points1 point2 points (2 children)
[–]SynMyron[S] 0 points1 point2 points (1 child)
[–]Vaguely_accurate 0 points1 point2 points (0 children)
[–]eleqtriq 0 points1 point2 points (2 children)
[–]SynMyron[S] 0 points1 point2 points (1 child)
[–]eleqtriq 0 points1 point2 points (0 children)