Search code examples
pythonstringstatements

statements and variables with first comma


The prompt:

Write a sequence of statements that finds the first comma in the string associated with the variable line, and associates the variable clause the portion of line up to, but not including the comma.

I am stuck. All I could understand and come up with was two statements (variables). First comma in the string associated with the variable line, makes me think of line equal line, and clause equal

Blockquote

line = line,
clause = "line"

Second attempt- still not working but I'm getting there now that I know to focus on find, index, or split

line = ""
clause = line.find("," [0[line]])

Attempt three, the split works but the issue is the line statement and it is by omitting the line statement that it finally works. Thanks so much!

clause = line.split(",")[0]

Solution

  • Okay, so it seems that you are having trouble understanding the actual statement, let's analyse what is being asked of you:

    Write a sequence of statements that finds the first comma in the string associated with the variable line (...)

    so, you create a variable line and associate a string to it, more specifically, a string that has a comma (that we are going to find).

    line = 'this is an example string, this is never going to be seen.'
    

    (...) and associates the variable clause the portion of line up to, but not including the comma.

    Now you need to create another variable clause that is going to be associated with that portion of the string BEFORE the comma (but excluding the comma), that is: 'this is an example string'

    clause = line.split(',')[0]
    

    All this code does is, it splits line where commas are and creates a list with the results, with the [0] you are accessing the first element of that list. Simple right?