Print Each Character of a String one at a time in Python
In this Python tutorial, we will learn how to print each character of a string one by one in Python. If you search this topic on the internet I am sure that you gonna find a lot of techniques to do this. But we always focus on the best and easiest way to solve any problem. Thus here we are not gonna show you multiple techniques to print each character from a string in Python.
In order to do this what we need just a string and a for loop to implement our operation.
Print each character from a string in Python one by one
Let’s take an example string:
this_string = "Hey I am CodeSpeedy!!!"
Now we want a Python program that will take each character from the string and print one by one at a time.
Let’s create our program.
this_string = "Hey I am CodeSpeedy" for character_index in this_string: print(character_index) # print each character at a time from string
H e y I a m C o d e S p e e d y
Explanation of the Python program:
We have taken a string. Then used a for loop to loop through the string.
In this for loop, we have printed the characters from the string one by one by its index starting from zero to the length of the string.
The character index of “H” -> 0 and “e” -> 1 , “y” -> 2 and so on.
We printed each character one by one using the for loop.
Print characters from the string with a certain time delay
import time this_string = "Hey I am CodeSpeedy" for character_index in this_string: print(character_index) # print each character at a time from string time.sleep(0.5)
It will give you the same output. But here each character will be printed with a time delay of half of a second.
It takes time in second as the parameter.