Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

Suppose I have a string: 997 668, now I need to remove anything before space i.e I need 668 as the output. I need a solution using regular expression. Now I am using the below:

x = '997 668'
x  = x.split(' ')[1]

Above also give the output but fails when there is only one number, if x = 555 then output comes blank which I dont want.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
151 views
Welcome To Ask or Share your Answers For Others

1 Answer

Instead of getting the first of one item or the second of two items, simply get the last item with [-1]:

>>> '996 668'.split()[-1]
'668'
>>> '668'.split()[-1]
'668'

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...