Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 112695
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T02:35:06+00:00 2026-05-11T02:35:06+00:00

I am a newbie to Python and have come across the following example in

  • 0

I am a newbie to Python and have come across the following example in my book that is not explained very well. Here is my print out from the interpreter:

>>> s = 'spam' >>> s[:-1] 'spa' 

Why does slicing with no beginning bound and a '-1' return every element except the last one? Is calling s[0:-1] logically the same as calling s[:-1]? They both return the same result. But I’m not sure what python is doing exactly. Any help would be greatly appreciated.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. 2026-05-11T02:35:07+00:00Added an answer on May 11, 2026 at 2:35 am

    Yes, calling s[0:-1] is exactly the same as calling s[:-1].

    Using a negative number as an index in python returns the nth element from the right-hand side of the list (as opposed to the usual left-hand side).

    so if you have a list as so:

    myList = ['a', 'b', 'c', 'd', 'e'] print myList[-1] # prints 'e' 

    the print statement will print ‘e’.

    Once you understand that (which you may already, it’s not entirely clear if that’s one of the things you’re confused about or not) we can start talking about slicing.

    I’m going to assume you understand the basics of a slice along the lines of myList[2:4] (which will return ['c', 'd']) and jump straight into the slicing notation where one side is left blank.

    As you suspected in your post, myList[:index] is exactly the same as myList[0:index].

    This is also works the other way around, by the way… myList[index:] is the same as myList[index:len(myList)] and will return a list of all the elements from the list starting at index and going till the end (e.g. print myList[2:] will print ['c', 'd', 'e']).

    As a third note, you can even do print myList[:] where no index is indicated, which will basically return a copy of the entire list (equivalent to myList[0:len(myList)], returns [‘a’, ‘b’, ‘c’, ‘d’, ‘e’]). This might be useful if you think myList is going to change at some point but you want to keep a copy of it in its current state.

    If you’re not already doing it I find just messing around in a Python interpreter a whole bunch a big help towards understanding these things. I recommend IPython.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 124k
  • Answers 124k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer No code solution: move the calendar from Exchange to SharePoint… May 12, 2026 at 1:14 am
  • Editorial Team
    Editorial Team added an answer Figured it out: navigator.userAgent.indexOf("WOW64") > -1 Evaluates to true if… May 12, 2026 at 1:14 am
  • Editorial Team
    Editorial Team added an answer I'm guessing it is a permissions issue. On Vista the… May 12, 2026 at 1:14 am

Related Questions

I am a newbie to python. I do have two modules. Model M1 and
As a newbie to Python, and mainly having a background of writing scripts for
I'm a newbie to programming. I had 1 semester of computer science (we used
Is is that I'm a newbie learning Ruby, or does it really have more

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.