Programming Questions

  • Newest
  • Popular Tags
  • Ask Question
  • Simple Symbols in Python
    Can someone please explain why we use range(0, len(str)): instead of just looping through str? From my flawed understanding if it the length of the string is 6 then looping through range(0, len(str) means you're looping through [0,1,2,3,4,5] and we're obviously not trying to loop through numbers, we're looping through strings.
    gmcintire posted this question on 11/28/15 | python, symbols
  • +
  • 3
  • -
  • Sometimes it is easier to iterate through the index of each character if we need to change the characters around it. For example: for i in range(len(str)): str[i - 1] = 'easy' is easier than for i in str: str[str.index(i) - 1] = 'hard'
  • +
  • 0
  • -
  • What does it mean 'we use'? Where do we use? If it is from someone's solution, please, write the whole example next time. Using indices is C/C++0x 'style' (imho). Instead of iterating characters in string we can iterate position of characters and after that get character by position. for ch in string: do_smth_with(ch) vs. for i in range(0, len(string)): ch = string[i] do_smth_with(ch)
    Log in to write an answer.