In this tutorial, we will learn how to print a word letter by letter in Python. This is a useful skill to know when working with text processing, as it can help with tasks like text manipulation, encryption/decryption, and other applications where individual characters need to be processed.
Step 1: Get a user-inputted word
First, let’s get word from the user that we want to print letter by letter. We will use the input()
function to obtain a string from the user.
1 |
word = input("Enter a word: ") |
Step 2: Print the word letter by letter using a for loop
We can use a for loop to iterate through each character in the string and print it.
1 2 |
for letter in word: print(letter) |
When using a for loop, the variable ‘letter’ will take the value of each character in the ‘word’ one at a time. The print()
function is called for each letter, outputting it to the console.
Full Code
1 2 3 4 |
word = input("Enter a word: ") for letter in word: print(letter) |
Example Output
Enter a word: Python P y t h o n
In this example, the user entered the word “Python”. The program prints each letter in the word on a separate line.
Conclusion
In this tutorial, we’ve learned how to print a word letter by letter in Python using the input()
function and a for loop. This technique is useful for text processing applications and can be expanded upon by adding more complex logic within the loop, such as counting the occurrences of a specific letter or modifying each character before printing it.