Traversing a string

advertisement
Strings in Python
String Traversal
Traversing a string
• Traversing just means to process every character in a string, usually from left end to right
end
• Python allows for 2 ways to do this – both useful but not identical
• if all you need is the value of each character in the string
for ch in stringvar:
will work fine. ch is given the value of each character from left to right, and can be used
in the for loop as you need
• The other way uses the location of the characters in the string
for ct in range(len(stringvar)):
makes the variable ct an integer which has all the values of the subscripts of the
characters in the string. If it is important that you know where a character is, this
is the form you use.
Download