Logo

Programming-Idioms

This language bar is your friend. Select your favorite languages!
  • Python

Idiom #286 Iterate over characters of a string

Print a line "Char i is c" for each character c of the string s, where i is the character index of c in s (not the byte index).

Make sure that multi-byte characters are properly handled, and count for a single character.

f = lambda i, c: f'Char {i} is {c}'
for e in enumerate(s): print(f(*e))
for i, c in enumerate(s):
    print(f'Char {i} is {c}')

Python strings are sequences of Unicode codepoints (not bytes).
(dorun (map-indexed (fn [i c] (println "Char" i "is" c)) s))

In Clojure a string is a list of characters

New implementation...