In python I want to show short term of string when I read from file and I want its show from back to fronts
Example. I read this string from file "I want to show some text from back"
I want to show like this "...some text from back"
How can I get string like this ? Thankyou
To get the last 4 words
>>> S = "I want to show some text from back"
>>> '...'+' '.join(S.split()[-4:])
'...some text from back'
' '.join("I want to show some text from back".split()[-4:])
The magic is the [-4:]
slice. That says to take list elements starting from the 4th to last. Nothing after the colon means continue until the end of the list.
You can slice the string, indexing backwards from the end.
s = "I want to show some text from back"
print "...%s" % (s[-20:])
>>... some text from back
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.