python - Remove the letters of a word at a time -
python - Remove the letters of a word at a time -
from string
t = "abcde"
i print in output list like
1.abcd 2.abc 3.ab 4.a
one approach trim off lastly character of string in each iteration of loop until there no characters left. can utilize string slicing substring of t
, instance t[:4]
gives first 4 characters of t
. more point, t[:-1]
gives last carachter of t
, want. can utilize len(t) check number of characters in t
, , utilize while
loop iterate until there no characters left.
t = "abcde" while len(t) > 0: t = t[:-1] if len(t) > 0: print t
just kicks, can in one-liner using join
, generator statements:
print "\n".join(t[:-i] in xrange(1,5))
python
Comments
Post a Comment