Loop through String in Python

Loop through String in Python

In this article, we will see how to loop through String in Python.

Iteration or looping means executing a set of statements till a given condition is met. In Python, we can loop over data structures to access their elements. A string is like an array of characters. We can loop through a string and display every character individually.

To achieve this, we can use the for loop and the while loop available in Python.

Use the for loop to loop through String in Python

The for loop is the most basic method to iterate over any sequence. With the help of the for loop, we can execute a set of statements once for every element or character present in a string.

For example,

Output:

H e l l o W o r l d
  • In the above example, we define the string Hello World.
  • Then, we start with the for loop using the variable character that will iterate over every alphabet in the defined string.
  • Finally, we print the value in this character variable in every iteration.

We can use different functions within the for loop to make this iteration more convenient. We will discuss these below.

Using the range() function

The range() function generates a sequence of numbers that starts from 0 by default and increments until it encounters the stop value.

We can use it to loop through a string and individually access its characters using their index.

For example,

Output:

H e l l o W o r l d

In the above example,

  • The len() function returns the length of the string.
  • The range() function generates the sequence of numbers from 0 to the length of the string.
  • Each number accesses the value at that position in the string.

Using the slicing [] operator

The slicing operator in Python helps in accessing different parts of a sequence or string.

We can use it to loop through a specific part of the string.

See the code below.

Output:

H
e
l
l
o

We can also use the slicing operator to iterate over a string in the reverse order.

We can do this in the following way.

Output:

d
l
r
o
W
o
l
l
e
H

Using the enumerate() function

The enumerate() function creates an enumerate object by adding a counter to an iterable. We can use this object in the for loop to iterate over the string by accessing its character and counter value at once.

For example,

Output:

H 0
e 1
l 2
l 3
o 4
5
W 6
o 7
r 8
l 9
d 10

Use the while loop to loop through String in Python

The while loop is another entry-controlled loop available in Python. Here, we can loop through to the length of the string and access characters using their indexes.

Note that in the while loop, we will have to increment the counter variable individually.

For example,

Output:

H
e
l
l
o
W
o
r
l
d

Conclusion

In conclusion, we have seen the use of the for loop and while loop to iterate through a string in Python. We understand that strings are inherently iterable so, it is easier to iterate through them using the for loop. Also, the functions enumerate() and range(), and the slicing operators have been demonstrated in looping through a string.

Was this post helpful?

Leave a Reply

Your email address will not be published. Required fields are marked *