开发者

How to remove leading and trailing spaces from strings in a Python list

i have a list:

row=['hi', 'there', 'how', ...........'some stuff is here are ','you']

as you can see row[8]='some stuff is here are '

if the last character is a space i would like to get everything except for the last character like this:

if row[8][len(row[8])-1]==' ':
  row[8]=row[8][0:len(row[8])-2]

this method is not working. can someone sugges开发者_如何学JAVAt a better syntax please?


row = [x.strip() for x in row]

(if you just want to get spaces at the end, use rstrip)


Negative indexes count from the end. And slices are anchored before the index given.

if row[8][-1]==' ':
  row[8]=row[8][:-1]


So you want it without trailing spaces? Can you just use row[8].rstrip?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜