python split and strip | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

python split and strip

hello what does [-1] do in this code lres = res.split(',')[-1].strip()

22nd Oct 2017, 1:00 PM
hossein hayati
hossein hayati - avatar
3 Answers
+ 5
[-1] is to get the last items in iterable ( An iterable is string, list, tuple or dict ) i.e. If a is "Kartikey" a[-1] is y
22nd Oct 2017, 1:04 PM
Kartikey Sahu
Kartikey Sahu - avatar
+ 2
-1 finds the last thing in your list and performs an action on it.
22nd Oct 2017, 1:03 PM
ghostwalker13
ghostwalker13 - avatar
+ 2
You can use strip like this. text = "!!!Hello ,,world..." for i in text: if i.strip('!,.'): #Only strip lets you removing every chars you want from everywhere. print(i,end="") print() for j in text: if j.rstrip('.'): #rstrip lets you removing chars from right side print(j,end="") print() for k in text: if k.lstrip('!'): #lstrip lets you removing chars from left side print(k,end="")
22nd Oct 2017, 2:52 PM
Ferhat Sevim
Ferhat Sevim - avatar