Useful Functions: String Functions | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Useful Functions: String Functions

I tried splitting 'hi hi hi' wherever it has 'h'. I got the print output as [",'i ','i ','i']. I did not get the logic behind the first entry of this list. Why am I getting 1st entry as empty? ===================== In [67]: x='hi hi hi' In [68]: print(x.split('h')) ['', 'i ', 'i ', 'i'] =====================

7th Apr 2017, 7:22 AM
deepak deo
deepak deo - avatar
1 Answer
+ 3
This is just the behaviour of split() if you specify sep. Analogous to: "If sep is given, consecutive delimiters are not grouped together and are deemed to delimit empty strings (for example, '1,,2'.split(',') returns ['1', '', '2'])." - from the Python documentation
7th Apr 2017, 11:49 AM
Tob
Tob - avatar