Loop through a string in Python

Loop through a string in Python

String is sequence of characters, each characters can be accessed using index. We can loop through the String and extract the characters each at a time.


for loop in string python

The 'for' loop in Python is different form the 'for' loop in the other programming languages.


In Python, the 'for' loop does not require any update to be done. It automatically update after each iteration.


Python 'for' loop syntax

for variable in string:
    statements

Example

Input

a = "Hello world!"
for i in a:
    print(i)

Output

H
e
l
l
o
 
w
o
r
l
d
!


 for  loop  through  a  string  with  range()   method

Loop over a string, like above can be done with the range() method.


Program

Input

a = "Hello world!"
for i in range(len(a)):
    print(a[i])

Output

H
e
l
l
o
 
w
o
r
l
d
!


Here we are using the iter() method to put all the characters in one list and then accessing each element of the list, which are the characters of the string.


It's another way, that I found to access characters from a string.

Program

Input

a = "Hello world!"
length = len(a)
for i in range(0, length):
    print(list(iter(a))[i])

Output

H
e
l
l
o
 
w
o
r
l
d
!


In looping over a string, range() method gives us the flexibility to slice the string.

Program

Input

b = "Hello world!"
for j in range(2, 5):
    print(list(iter(b))[j])

Output

l
l
o

Interval can be introduced while looping over a  string with the range() method.

Program

Input

b = "Hello world!"
length = len(b)
for step in range(0, length, 2):
    print(list(iter(b))[step])

Output

H
l
o
w
r
d


'for' loop through a list with enumerate() method

The enumerate() method enumerates the string, with numbers.


Following is an example to enumerate the string.

Program

Input

b = "Python"
print(list(enumerate(b)))

Output

[(0, 'P'), (1, 'y'), (2, 't'), (3, 'h'), (4, 'o'), (5, 'n')]


Here is an example, how you can fetch the content, got after enumerating the string. Both, characters and the enumerated value.

Program

Input

b = 'Python'
for i, j in list(enumerate(b)):
    print("Index: ", i, " Value: ", j
)

Output

Index:  0  Value:  P
Index:  1  Value:  y
Index:  2  Value:  t
Index:  3  Value:  h
Index:  4  Value:  o
Index:  5  Value:  n



Iterating through a string Using List comprehension

By using the concept of list comprehension we can loop through a string, and can store each character in a list.


Here is how you can loop through a string using list comprehension.

Program

Input

a = "Hello world!"
[print(char) for char in a]

Output

H
e
l
l
o
 
w
o
r
l
d
!


Conditional list comprehension with  a string

Condition is placed at the end, if the condition is True then only the expression is executed.


Here is an example, which will not print the character 'l', it will print all the characters in the string except 'l'.

Program

Input

a = "Hello world!"
[print(char) for char in a if char != 'l']

Output

H
e
o
 
w
o
r
d
!


Function and list comprehension with a string

Introducing a function at the beginning of the list comprehension, is also possible.


Example:- Here first we define a function 'concat()', which will print all the characters passed to it except the character 'l'. And this function is used in the list comprehension, to remove character 'l' from a string.

Program

Input

s = "Hello world"

def concat(char):
    if char != 'l':
        print(char)
        
[concat(char) for char in s]


Output





Conclusion

String is a collection or sequence of characters, and each characters are accessed by index.

Here we have used 'for' loop to iterate over characters of a string and enumerated string.





Previous                          Next

If you have any query, feel free to ask.

إرسال تعليق

If you have any query, feel free to ask.

Post a Comment (0)