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.


import_contacts

You may also like:

Related Posts

  • 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 […]

  • Get variable name as String in Python
    01 October

    How to get variable name as String in Python

    Table of ContentsWhat are variable names in Python?How to get a variable name as a string in Python?In Python 2.xIn Python 3.x In Python, there are several ways to convert a string to a variable name, but the reverse process is not exactly utilized so often in the programming world. This article focuses on how […]

  • Python hex to bytes
    31 May

    Convert Hex to bytes in Python

    Table of ContentsUse codecs.decode() function to Convert Hexadecimal value to Byte in PythonUse bytes.fromhex() function to Convert Hexadecimal value to Byte in PythonUse the binascii Module to Convert Hexadecimal value to Byte in Python 💡 Outline You can use codecs’s decode() function to convert Hex to bytes in python. This method works for Python 2.x […]

  • Remove commas from String in Python
    23 May

    Remove comma from String in Python

    Table of ContentsUsing replace() function to remove comma from string in PythonUsing re or RegEx package function to remove comma from string in Python 💡 Outline You can remove comma from string in python by using string’sreplace() method. Syntax: [crayon-61a05e1bd7f0c447444407/] In this tutorial, we will take a sample string with a couple of commas in […]

  • Add character to String in Python
    21 May

    Add character to String in Python

    Table of ContentsUsing the + operatorUsing the join() functionUsing the % operatorUsing the format() functionUsing the f-strings Strings are considered as a sequence of characters in programming. In Python, string objects are considered immutable. What this means is that string objects cannot be changed once created, and a new string is created with a different […]

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.