Reputation: 1187
I have a string that looks like this
name = '1/23/20151'
And now I want to remove just the trailing 1, at the end of 2015. So that it becomes
1/23/2015
So i tried this
sep = '2015'
name = name.split(sep, 1)[0]
but this removes the 2015 also, I want the 2015 to stay, how could I do this.
Thanks for the help in advance.
EDIT
Sorry I didn't fully explain the problem I have two strings the one previously mentioned and a noraml date '1/22/2015'
and I loop through and only want to remove this extra character if it is there which is why name = name[:-1]
doesn't work.
Upvotes: 1
Views: 107
Reputation: 1450
just do this
name = name[:-1]
That should do it.
If you only want to remove the fifth digit after the year, I'd do this:
name = name.split('/')
name = '/'.join([name[0],name[1],name[2][:4]])
Upvotes: 2
Reputation: 4570
name = name.rstrip('1')
will only remove trailing '1'
name = '1/23/20151'
name = name.rstrip('1') # 1/23/2015
'1/23/2015'.rstrip('1') # 1/23/2015
Upvotes: 4
Reputation: 3453
List slicing can easily accomplish this:
>>> name[:-1]
>>> '1/23/2015'
Upvotes: 1