repeat code python code example

Example 1: how to reapete the code in python

for i in range(4):  # 4 = number of times you want to run the code
  print("Hello World")

Example 2: how to repeat if statement in python

def main():
    while True:
        again = raw_input("Would you like to play again? Enter y/n: ")

        if again == "n":
            print ("Thanks for Playing!")
            return
        elif again == "y":
            print ("Lets play again..")
        else:
            print ("You should enter either \"y\" or \"n\".")

if __name__ == "__main__":
    main()

Example 3: python itertools repeat()

# How to use it:
# repeat(var, amount)

# repeat() is basically like range() but it gives an extra arg for a var

from itertools import repeat

for x in repeat("Spam? Or Eggs?", 3):
    print(x)
> "Spam? Or Eggs?"
> "Spam? Or Eggs?"
> "Spam? Or Eggs?"

Example 4: how to make a repeater in python 3

a_string_repeated_to_target = a_string_repeated[:target_length]