v26.docx - Instead of returning the entire string-9:0 returns the empty string This is because the second number in a slice must correspond to a

v26.docx - Instead of returning the entire string-9:0...

This preview shows page 1 out of 1 page.

Instead of returning the entire string, [-9:0] returns the empty string "". This is because the second number in a slice must correspond to a boundary that comes after the boundary corresponding to the first number, but both -9 and 0 correspond to the left-most boundary in the figure. If you need to include the final character of a string in your slice, you can omit the second number: >>> flavor[-9:] 'apple pie' Strings Are Immutable To wrap this section up, let’s discuss an important property of string objects. Strings are immutable, which means that you can’t change them once you’ve created them. For instance, see what happens when you try to assign a new letter to one particular character of a string: >>> word = "goal" >>> word[0] = "f" Traceback (most recent call last): File "", line 1, in word[0] = "f" TypeError: 'str' object does not support item assignment Python throws a TypeError and tells you that str objects don’t support item assignment. Note The term str is Python’s internal name for the string data type. If you want to alter a string, you must create an entirely new string. To change the string "goal" to the string
Image of page 1

You've reached the end of your free preview.

Want to read the whole page?

  • Spring '15
  • Letter case, String literal, Lower case, entire string

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern

Stuck? We have tutors online 24/7 who can help you get unstuck.
A+ icon
Ask Expert Tutors You can ask You can ask You can ask (will expire )
Answers in as fast as 15 minutes