Reputation: 15
Is there a way in python to store the part I sliced and print only the part I sliced? Meaning on the example below I sliced out "school". I want to just print "school". I will be trying to work on a text file later, where I will just need part of each sentence, but just trying to figure out if its doable with slicing.
word= "the teacher am my school"
a= word[0:-6]
Upvotes: 0
Views: 150
Reputation: 11686
Your interpretation of the wording is what is causing confusion. The slice refers to the part that is referenced (or remains), not the part that is removed.
'word'[0] == 'w' # the slice is 'w' not 'ord'
Upvotes: 0
Reputation: 281167
print a
I'm not sure what your confusion is.
Update: "school"
is the part you didn't slice. If "school"
is the part you're interested in, you want
a = word[-6:]
If you want both parts, slice them separately:
notschool, school = word[:-6], word[-6:]
Upvotes: 4
Reputation: 1085
why not slice it directly, then ?
a = word[-6:]
will give you 'school'
Upvotes: 1