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.

Related Posts

  • Check if variable is String in Python
    13 January

    Check if variable is String in python

    Table of ContentsHow to check if a given variable is of the string type in Python?Using the isinstance() function.Using the type() function.Check if function parameter is String In this post, we will see what is a string in Python and how to check whether a given variable is a string or not. There are many […]

  • 09 January

    Convert set to String in Python

    Table of ContentsWays to convert set to string in PythonUsing the str() functionUsing the repr() functionUsing the join() functionUsing the map() functionConclusion Sets are an unordered collection of elements in Python. It does not contain duplicate elements and can be defined using the set() function in Python. Ways to convert set to string in Python […]

  • CharAt in Python
    08 January

    CharAt in Python

    Table of ContentsCharAt() in javaAlternative of charAt() in PythonIterate through String and print its character In this tutorial, we will see about CharAt in Python. If you are already well verse in java and might be looking for charAt() alternative in Python, then you are at right place. In Python, you can get character of […]

  • Convert dict to String in Python
    17 December

    Convert dict to string in python

    Table of ContentsHow to convert dict to string in python?Using str() method.Using the pickle module.Using the for loop.Using the json.dumps() method. In this post, we will see how to convert dict to String in Python. Python contains several data structures, and more often than not, there is a need for the conversion of one type […]

  • Split String with multiple delimiters
    17 November

    Split String With Multiple Delimiters In Python

    Table of ContentsSplitting a string in PythonWays to split a string in PythonUsing the split() and replace() functionsUsing the regular expressions In this post, we will see how to split String with multiple delimiters in Python. Splitting a string in Python When we talk about splitting a string, it means creating a collection of sub-strings […]

  • String to char Array in Python
    06 November

    Convert String to Char array in Python

    Table of ContentsConvert String To Char Array In Python Using For LoopConvert String To Char Array In Python Using The list() ConstructorConvert String To Char Array In Python Using The extend() MethodConvert String To Char Array In Python Using The += OperatorConvert String To Char Array In Python Using Packing And UnpackingConvert String To Char […]

Leave a Reply

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

Subscribe to our newletter

Get quality tutorials to your inbox. Subscribe now.