Programming Questions

  • Newest
  • Popular Tags
  • Ask A Question
  • Python Reverse Text 2 Colons
    Hi guys, I'm new to this website and to programming. I did the first easiest question, which is reversing the text. I've done the question myself through looping, but I checked other's work and it was shocking that they answered the question with 1 line. The code is below:
    def FirstReverse(str):
      return str[::-1]    
        
    # keep this function call here  
    # to see how to enter arguments in Python scroll down
    print FirstReverse(raw_input())
    
    My question now is, how does the 2 colons work? I know that it's start, stop and step, but this doesn't clarify much. Is it somewhat like looping? From what I know, it's slicing, so does it slice every -1? If so, then it would only show the last one then. It would be very much appreciated if the concept of this will be explained. Again, I'm sorry for being new and for my bad English as it is not my native language.
    snobaakp1 posted this question on 2/26/14 | python
    Answers
  • +
  • 7
  • -
  • Hi snobaakp1! :) Hope you are enjoying your new learning experience in programming! To answer your question, slicing works like this. list_or_string[A:B:C] The area inside the square brackets can hold 3 things, each separated by a colon ( : ) The first item, A, tells the list slicing where the list or string (a string is just a list of characters) should start. So if A is 2, then list slicing will show the items in the list starting from items with an index of 2. So for example, "python"[2:] will show => "thon" It starts from the element with index of 2. If A is not mentioned, like in the First Reverse question, then it is set to the default index of 0. Which is the first element. The second item is list slicing, B, tells the list slicing where to stop. So if B is 4, then the list slicing will stop 'before' it reaches the item with index of 4. So for example, "python"[0:4] will show => "pyth" It stops before it reaches the item with index of 4, which is the letter "o". If item B is empty or not mentioned, then it is assumed that the list slicing will continue until the last element. Like in the example I showed previously: "python"[2: ] will show => "thon" The third item in list slicing, C, tells the list slicing how many steps to take each time when it shows the list slicing, and it also tells which direction the list slicing should go. If C is 1, then it will slice 1 step at a time. So "python"[ : : 1] will show "python". It gives you a slice with all the letters before you go through the string one letter at a time. If "python"[ : : 2], then it will show "pto". It ignores "y", "h", and "n" It takes 2 steps each time, starting from the first element in the string. And it ignores every second letter. If item C is negative, like in the example of First Reverse question, then it will take each step backwards. So "python"[ : : -1] will show "nohtyp". "python"[ : : -2] will show "nhy". It goes from back to front. Hope you get what I mean. :)
  • +
  • 2
  • -
  • Also, since English is not your native language. Let me explain a bit about the terminology. The word "slice" in English means: 1. a thin, flat piece cut from something 2. or cut out a small piece from something That is what list slicing does. You can slice or cut a list into pieces. And then you display the pieces in whatever order you want. It's just like cutting or slicing up a pizza. You can choose how you arrange the pieces. The 3 items in list slicing allow you to decide how many slices you want to cut out of the list, and how you want to arrange those slices. Hope you get what I mean...
  • +
  • 0
  • -
  • List slicing may be a little complicated for begginers, so I'll explain:
    string[where to start slice:where to end slice:step]
    where step is kind of like changing your string following a specific pattern. If step is 2 and str is 'coderbyte', then we change str from the original to every second letter. We get:
    >>>'coderbyte'[::2]
    cdrye
    Note that it starts counting from the first character. if the step is -1, then we are telling the computer to change the string to the string BACKWARDS(Yes, i don't really understand the logic of that either), so we gte our string backwards.
    >>>'coderbyte'[::-1]
    etybredoc
    Log in to write an answer.