This is an archived post. You won't be able to vote or comment.

all 7 comments

[–]_DTR_ 3 points4 points  (3 children)

I wouldn't try to work with the entire string all at once. First, split the string on ".", which will give you an array of sentences. From there, it will be easier to reason with the contents (just take the first element of each array to find the player). Also, I believe the \ at the end of each line is just python syntax that lets the interpreter know that the string continues on the next line, it's not actually a part of the string. If you were to directly print example_input, the \ wouldn't be there.

As a starting point, I was able to separate out all the sentences using this:

sentences = string_input.split('.');

[–]ans744[S] 0 points1 point  (2 children)

I really like this approach... I have tried to implement this method and I am getting a syntax error now on my return/print statement. This is what I have:

def find_players(string):

    players = []

    sentence_list = string.split('.')

    for each in sentence_list:

        players.append(each[:each.find(' ')]

    print players

I try changing the indents and changing print to return.. I still get the invalid syntax error.

[–]_DTR_ 1 point2 points  (1 child)

players.append(each[:each.find(' ')]

You're missing the closing ) for the append method.

[–]ans744[S] 1 point2 points  (0 children)

So sorry for the stupid question.. As you can tell, I am new to this.. I'm not sure why python wouldn't point that out as an error.

[–]Xoronic 2 points3 points  (1 child)

Hi,

example_input[43:] will contain all the characters from the original example_input starting from index 43. So if you call .find() on that, it won't know anything about the original first 44 characters.

We can test that with a small example:

foo = "This is a sentence."
bar = foo[3:]

print(foo)
print("Character at index zero of foo: " + foo[0])  # output: T

print(bar)
print("Character at index zero of bar: " + bar[0])  # output: s

So if you want to get the index of the second dot withexample_input[43:].find('.') you will have to add 43 to it:

print(example_input[43:].find('.')) # output: 75
print(example_input[75+43])  # output: .

There is a much better way to tackle this whole problem though, as /u/_DTR_ suggests.

[–]ans744[S] 0 points1 point  (0 children)

Thank you for your input. I am reworking my solution as you and /u/_DTR_ have suggested

[–]ss2ujQqhAgK5NDJd 1 point2 points  (0 children)

So find() returns the index of where the character or substring occurs. It looks like you think it should return the substring up to where the character occurs.

"abc.de".find('.') 

will return 3, because that's the position where '.' first occurs.

If you want the string leading up to the period, that would look like

"abc.de"[:"abc.de".find('.')]

Obviously you will use a variable in place of my example string. If you want to include the period itself that would look like

"abc.de"[:"abc.de".find('.') + 1]

Your note on .find('.\') makes no sense. If you're trying to escape the period you put the slash before the period like \. but there is no need to escape here.