Iterate over Characters of a String
To iterate over the characters of a string in Python, we can use use for loop statement.
The following code snippet shows how to iterate over the characters of a string myString
using for loop.
</>
Copy
for ch in myString:
//code
Example
In the following program, we take a string in name
variable, and iterate over the characters of the string using for loop.
main.py
</>
Copy
myString = 'apple'
for ch in myString:
print(ch)
Output
a
p
p
l
e
Reference
In the above program(s), we have used the following Python concepts. The links have been provided for reference.
- Initialize string using single quotes
- For Loop
- Python print() builtin function
Conclusion
In this Python Tutorial, we learned how to iterate over the characters of a string in Python using for loop statement.