String Manipulations

MIT 6.0001 Introduction to Computer Science and Programming in Python, Fall 2016
View the complete course: ocw.mit.edu/6-0001F16
Instructor: Dr. Ana Bell
This in-class question demonstrates the use of string indices in Python.
License: Creative Commons BY-NC-SA
More information at ocw.mit.edu/terms
More courses at ocw.mit.edu

Пікірлер: 18

  • @akbarrauf2741
    @akbarrauf27417 жыл бұрын

    thank you ,mit

  • @brainstormingsharing1309
    @brainstormingsharing13093 жыл бұрын

    Absolutely well done and definitely keep it up!!! 👍👍👍👍👍

  • @AnmolKumar-dh3lh
    @AnmolKumar-dh3lh Жыл бұрын

    great teacher I am so jealous of students at MIT

  • @1nd93dk3
    @1nd93dk32 жыл бұрын

    lol notice the reference here. the lecture hall they were in was 26-100

  • @williamwilliams1000
    @williamwilliams10007 жыл бұрын

    00:25

  • @shantanubaghel2058

    @shantanubaghel2058

    6 жыл бұрын

    Real MVP

  • @Zoronoa01

    @Zoronoa01

    5 жыл бұрын

    Thanks !

  • @peterpace3379
    @peterpace33796 жыл бұрын

    Meedaaam kya padhati jhakaaaasss

  • @AnmolKumar-dh3lh
    @AnmolKumar-dh3lh Жыл бұрын

    the answer is actually wrong in the latest python version

  • @gabrieleherr348
    @gabrieleherr3483 жыл бұрын

    Wait, I thought strings were immutable...

  • @bee_irl

    @bee_irl

    3 жыл бұрын

    That isn't really relevant here. To my understanding, what that means is that you can't change the contents of the memory once you created the variable, but you can still change the variable - doing so will result in it creating a new value somewhere else in the memory, leaving the original behind (instead of modifying it). Someone correct me if I'm wrong.

  • @gabrieleherr348

    @gabrieleherr348

    3 жыл бұрын

    ​@@bee_irl Right, so the variable can change but the string itself can't...

  • @algemmegla9002

    @algemmegla9002

    3 жыл бұрын

    >>> a = "Hello world" >>> a 'Hello world' >>> a[0] = "M" # attempting to modify the string. Traceback (most recent call last): # strings are immutable. Doesn't work. File "", line 1, in a[0] = "M" >>> a_new_variable = "" # an empty string, assigned to a new variable name. >>> a_new_variable += a[3:9] # slicing "a". a_new_variable set to itself plus the slice information from "a". >>> a_new_variable 'lolo wor' So "a_new_variable" was "created", and "a" wasn't changed in place. "a" is still Hello world In other words, you can change a string by creating a whole new variable name, and assigning it to an altered version of the original string.

  • @sreehari563

    @sreehari563

    3 жыл бұрын

    @@algemmegla9002 to anyone from future reading along, there is one more point to algem's reply. This is how Python handles a mutable thing like lists vs how it handles immutable thing like strings. Lets say you create a string and assign it to a, >>a="hello". python creates a string "hello" store it somewhere in memory and make variable a point to it. Now when you do >>b="hello", python makes the new variable b point to the org string that was created before., meaning there is only one copy of "hello" in memory and both a and b point to it. This is how python works on immutable objects like strings. Also, as you can see from above, there is no way to change the string a="hello" without affecting b. Hence Python blocks or doesn't allow to change strings. But with mutable objects like lists, when you first do a=[1,2,3,4] and then do b=[1,2,3,4] python has to create a new list [1,2,3,4] (other than the one already present in memory ) and then make the new variable b point to it. So now there are two distinct variables a and b pointing to 2 separate copies of [1,2,3,4].Hence you can can safely change list- a without affecting b. Pro tip- remember this when you write memory efficient programs in python, DON'T KEEP creating COPIES OF PREEXISTING LISTS. Based on the length of your org list, you might run of memory pretty soon and hang your system. Please scroll down and read section 10.10 and also10.11 over here- www.greenteapress.com/thinkpython/html/thinkpython011.html

  • @algemmegla9002

    @algemmegla9002

    3 жыл бұрын

    @@sreehari563 I see, so assigning a different variable name to the same string "is not" referring to the same string in memory. It points to a "new copy" of the original string. (Edit: Redaction below) In the case of lists and dictionaries, assigning a new variable name to the old variable name points to the "same list or value:key pair" in memory? Thank you for clearing that up. [Edit: **See below comments for follow up redaction on this topic.**] Just to revisit Gabriel's question, I think the confusion is that you can take an empty string and assign index positions from a different string. *But the act of assigning indexes from an "old string" to a "new string" is misleading and appears to be making changes or to mutate the new string.*