1) What do the values in the square brackets do?
See Lists in the tutorial for a full explanation, but briefly:
A simple value in the square brackets is an index. If you have a list a = ['a', 'b', 'c', 'd']
, then a[0]
is 'a'
, a[1]
is 'b'
, and so on.
A pair of values separated by a colon is a slice: it gives you not a single value from the list, but a smaller list consisting of all of the values. So, a[1:3]
is ['b', 'c']
.
You can leave off the start or end of a slice, so a[1:]
is ['b', 'c', 'd']
.
You can use negative numbers for both indices and slices, meaning to count from the end, so a[-1]
is 'd'
, a[-2]
is 'c'
and a[-3:-1]
is ['b', 'c']
.
So, when you do this:
line.rsplit(",",1)[1:]
If you look up str.rsplit
, you can see that line.rsplit(",", 1)
returns a list of 2 strings—everything up to the last comma, and everything after the last comma—unless there were no commas, in which case it returns a list of 1 string—the whole of line
.
The [1:]
means that you want everything from the second element on. So, if there was a comma, you get a list of 1 string—everything after the last comma; if there was no comma, you get an empty list.
Let's step through your edited code piece by piece.
>>> s = ',000'
>>> split_s = s.rsplit(",", 1)
>>> split_s
['000']
>>> var = split_s[1:]
>>> var
['000']
>>> var0 = var[0]
>>> var0
'000'
>>> svar0 = string(var0)
TypeError: 'str' object is not callable
>>> # ignore that part, I guess?
>>> joined = ",".join(var0)
'0,0,0'
>>> var = "{}".format(joined)
>>> var
'0,0,0'
Notice that two of these steps do absolutely nothing, while one of them raises an exception, and I'm not sure what they were intended to do. And the final result (removing the part that raises an exception) doesn't demonstrate what you claimed you were trying to fix. So your question is completely unanswerable.
At any rate, if you don't understand how your code breaks down into these steps, you shouldn't be writing dense code. Write things as exactly one simple expression per line, make sure you understand exactly what each one is doing (e.g., by printing out the results, as I did), and get all of them working. After that, you can try to make it more concise again.