Python is a highly versatile language that’s used in a diverse range of applications. This tutorial will help you learn how to extract the first letter of a word in Python.
This functionality can be useful in numerous scenarios, like word games, data processing, and text analysis. Let’s dive into the process step-by-step to see how we can do it.
Step 1: Assign a word to a variable
At first, we must assign a word to a variable. This variable will represent the word from which we wish to extract the first letter. Here’s how to do it:
1 |
word = "Python" |
Step 2: Index the word
Python allows us to index strings. Indexing is a technical term for getting the character at a particular position. In Python, indexing starts from 0. So, to get the first letter, we use the 0th index. Here’s how to extract the first letter:
1 |
first_letter = word[0] |
Step 3: Print or use the first letter
Now that we’ve stored our first letter in the ‘first_letter’ variable, we can print it or use it as per our requirement. Here’s how to print the letter:
1 |
print(first_letter) |
Full Code:
Here’s the full code for extracting the first letter from a word.
1 2 3 |
word = "Python" first_letter = word[0] print(first_letter) |
P
Conclusion:
In this tutorial, we learned how to extract the first letter from a word in Python. The method leverages Python’s indexing feature, which is a powerful tool within the language for manipulating and retrieving data. By using indexing, we built a short script that allows us to get the first letter of any word rapidly. This functionality will likely come in handy during your Python coding adventures!