Here's an example of a for loop in Python where the loop body iterates over a string:

my_string = 'Hello, world!'

for char in my_string:
    print(char)

In this example, we define a string variable my_string containing a simple greeting 'Hello, world!'. We then use a for loop to iterate over each character in the string and print it. In each iteration of the loop, the variable char is assigned the next character from my_string. Consequently, this loop prints each character of the string, effectively iterating over the string.

Python For Loop Example: Iterating Over a String

原文地址: https://www.cveoy.top/t/topic/mZFf 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录