How to Break a Loop in Python?

In this tutorial, we will learn how to break/terminate loop with the help of examples in Python? By Pankaj Singh Last updated : April 13, 2023

Break/Terminate a Python Loop

To beak/terminate a Python loop, use the break statement with a condition from where you want to terminate it.

Terminate a Python Loop - Examples

Consider the following examples to understand the concept of breaking a Python Loop.

Example 1

In the given example, loop is running from 1 to 10 and we are using the break statement if the value of i is 6. Thus when the value of i will be 6, program's execution will come out from the loop.

for i in range(1,11):
    if(i==6):
        break
    print(i)

Output

1
2
3
4
5

Example 2

In this example, we are printing character by character of the value/string “Hello world” and terminating (using break), if the character is space.

for ch in "Hello world":
    if ch == " ":
        break
    print(ch)

Output

H
e
l
l
o

To understand the above programs, you should have the basic knowledge of the following Python topics:

Python Basic Programs »

Related Programs

Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.