Big.Bang
Big.Bang

Reputation: 33

Slice string in python

I just want to slice string from the beginning.As like I have a sentences:

"All the best wishes"

I want to get

"the best wishes" , "best wishes", "wishes".

Any solution please,Thanks!

Upvotes: 1

Views: 513

Answers (5)

sampwing
sampwing

Reputation: 1268

s = "All the best wishes"
[' '.join(s.split()[x:]) for x in xrange(1, len(s.split()))]

Upvotes: 1

Adam Jurczyk
Adam Jurczyk

Reputation: 2131

Eh, pythoners;]

You can always do it with simple loop and function:

def parts(s, fromstart=True):
    sl, slp, idx = s.split(), [], 0 if fromstart else -1
    while len(sl)>1:
        sl.pop(idx)
        slp.append(' '.join(sl))
    return slp

s = 'All the best wishes'
parts(s) # -> ['the best wishes', 'best wishes', 'wishes']
parts(s,False) # -> ['All the best', 'All the', 'All']

Upvotes: 1

fransua
fransua

Reputation: 1608

a = "All the best wishes"
[a.split(None,x)[-1] for x in xrange(1, len (a.split()))]

Upvotes: 1

utdemir
utdemir

Reputation: 27216

>>> words
['All', 'the', 'best', 'wishes']
>>> [" ".join(words[i:]) for i in range(len(words))]
['All the best wishes', 'the best wishes', 'best wishes', 'wishes']
>>> [" ".join(words[i:]) for i in range(len(words))][1:]
['the best wishes', 'best wishes', 'wishes']

Upvotes: 5

Karoly Horvath
Karoly Horvath

Reputation: 96268

use:

searchWords.extend([' '.join(words[i:]) for i in xrange(1, len(words))])

Upvotes: 4

Related Questions