About 174,000 results
Open links in new tab
  1. Iterating each character in a string using Python

    Aug 29, 2022 · A string is inherently a list of characters, hence 'map' will iterate over the string - as second argument - applying the function - the first argument - to each one. For example, here I …

  2. Iterate over a string 2 (or n) characters at a time in Python

    Jul 22, 2009 · Earlier today I needed to iterate over a string 2 characters at a time for parsing a string formatted like "+c-R+D-E" (there are a few extra letters). I ended up with this, which …

  3. Python: looping over characters in a string when string is changing

    Jul 27, 2014 · 2. Your loop doesn't iterate over s. It iterates over the value s had when the loop started. When you do for item in iterable, this is what Python does: It gets a reference to the …

  4. Python: loop over consecutive characters? - Stack Overflow

    Mar 22, 2016 · You have a constant in the string module called ascii_lowercase, try that out: >>> from string import ascii_lowercase Then you can iterate over the characters in that string. >>> …

  5. Preventing a Python For-loop from iterating over a single string by ...

    When the variable is a single string , the loop iterates it by char. text = function() # the function method can return a single string or list of string for word in text: print word+"/n" # When it is a …

  6. How do I iterate through a string in Python? - Stack Overflow

    Apr 2, 2015 · import string for c in string.lowercase: print c How get letter frequencies with some efficiency and without counting non-letter characters: import string sample = "Hello there, this …

  7. Best way to loop over a python string backwards

    Aug 9, 2015 · What is the best way to loop over a python string backwards? The following seems a little awkward for all the need of -1 offset: string = "trick or treat" for i in range(len(string)-1, 0 …

  8. Python Iterate Over String - Stack Overflow

    Apr 25, 2017 · This probably works but it involves an unnecessary if-else-condition which would be inefficient for very long strings (unnecessary iteration over excess characters and …

  9. Python - Looping through a string pulling 1 character at a time …

    Oct 1, 2012 · Yes, it is the official styling guide for Python code described in Python Enhancement Proposal no. 8 aka PEP8. What you've written looks like Java code. What you've written looks …

  10. Python: Iterate over string with while loop - Stack Overflow

    Sep 5, 2013 · I'm trying to delete all the characters at the end of a string following the last occurrence of a '+'. So for instance, if my string is 'Mother+why+is+the+river+laughing' I want …