How to Loop Through the Characters of a Python String
This is going to be a quick guide where we walk through how we can extend our knowledge of looping in python and we see that the for in loop can also be used with strings.
Guide Tasks
  • Read Tutorial
  • Watch Guide Video
  • Complete the Exercise
Video locked
This video is viewable to users with a Bottega Bootcamp license

So if I have a string that contains a portion of the alphabet. I could say alphabet and inside of that put abcdef.

alphebet = 'abcdef'

and if I want to loop through each one of those items a 4 in the loop allows me to access them just like they were in a collection because if you think about it a string is really just a collection of characters so it's almost like you can think about a string is a list in a sense. When it comes to iterating over it so I could think of the first element being the character a, the second one being the Character b and so on and so forth and so with that in mind it means that we can do something like

for letter in alphabet:

and then I can simply print this out so I can say print letter just like this.

print(letter)

And now if I run this you can see that it prints out each one of the elements exactly like how we walk through with the data collections.

large

Code

alphabet = 'abcdef'

for letter in alphabet:
  print(letter)