How do you loop through a string using a for loop in Python?

You can use a for loop to iterate through each character in a string. For example:

string = "Hello World"
for char in string:
    print(char)

The result is output as:

H
e
l
l
o

W
o
r
l
d

You can process each character based on actual needs, such as counting the occurrence of characters or replacing specific characters.

bannerAds