Exploring Python: A Simple for Loop Example

.

Python Code Example

        # Define a string variable x with the value "Test5"
        x = "Test5"

        # Use a for loop to iterate over each character in the string x
        for i in x:
            # Print "Hello World" for each character
            print("Hello World")
    

Explanation:

  1. x = "Test5": Initializes a variable x with the string value "Test5".
  2. for i in x:: This line starts a for loop that iterates over each character in the string x. In each iteration, the current character is assigned to the variable i.
  3. print("Hello World"): Inside the loop, this line prints the string "Hello World" for each character in the string x. Since the loop is iterating over the characters of "Test5", the "Hello World" message will be printed five times, once for each character.

Output:


        Hello World
        Hello World
        Hello World
        Hello World
        Hello World
    

Post a Comment

0 Comments