Raw string vs Python string
r'","'
The r is to indicate it's a raw string.
How is a raw string different to a regular python string?
The special characters lose their special meaning inside a raw string. For example \n
is a newline character inside a python string which will lose its meaning in a raw string and will simply mean backslash followed by n.
string.split()
string.split()
will break and split the string
on the argument that is passed and return all the parts in a list. The list will not include the splitting character(s).
string.split('","')
will break and split the string on every ","
and return all the broken parts in a list excluding ","
Eg:
print 'Hello world","there you are'.split(r'","')
Output:
['Hello world', 'there you are']
split()
can do even more...
You can specify how many parts you want your string to break into by passing in an extra parameter.
Lets consider this string: 'hello,world,there,you,are'
- Split on all commas and break into n+1 parts where n is the number of commas:
>>>print 'hello,world,there,you,are'.split(',')
['hello', 'world', 'there', 'you', 'are']
- Split on first comma and break only into 2 parts.
>>>'hello,world,there,you,are'.split(',',1)
['hello', 'world,there,you,are']
- Split on first and second comma and break into 3 parts. And so on...
>>>'hello,world,there,you,are'.split(',',2)
['hello', 'world', 'there,you,are']
And even more...
From the docs:
If splitting character(s) i.e separator is not specified or is None, a different splitting algorithm is applied: runs of consecutive whitespace are regarded as a single separator, and the result will contain no empty strings at the start or end if the string has leading or trailing whitespace. Consequently, splitting an empty string or a string consisting of just whitespace with a None separator returns [].
For example,
>>>' 1 2 3 '.split()
['1', '2', '3']
>>>' 1 2 3 '.split(None, 1)
['1', '2 3 ']
>>>''.split()
[]
>>>' '.split()
[]
>>>' '.split(None)
[]
And even...
.
.
.
What?
Isn't it enough that you are looking for more? Don't be so greedy :P.
Just question yourself?
, it will make you non-greedy :D (You will get the joke if you know regular expressions)