+ 1
python split and strip
hello what does [-1] do in this code lres = res.split(',')[-1].strip()
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
+ 2
-1 finds the last thing in your list and performs an action on it.
+ 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="")