Printing initials of a name is a basic operation when it comes to working with names in Python. This tutorial will guide you on how to do that easily.
Steps:
1. Get the name as input from the user:
To get the input from the user, you can use the input() function in Python. The input function will get the name from the user as a string.
Example code:
1 |
name = input("Enter your name: ") |
2. Split the name into words:
The next thing you need to do is to split the name into words using the split() function. This will allow you to work with each word separately.
Example code:
1 |
words = name.split() |
3. Get the first letter of each word:
Once you have the words, you can get the first letter of each word using indexing. This can be done by specifying the index of the first letter in the word.
Example code:
1 2 3 |
initials = "" for word in words: initials += word[0] |
4. Print the initials:
Finally, you can print the initials using the print() function.
Example code:
1 |
print("Initials:", initials) |
Full Code:
1 2 3 4 5 6 |
name = input("Enter your name: ") words = name.split() initials = "" for word in words: initials += word[0] print("Initials:", initials) |
Output:
Enter your name: Tomasz Decker Initials: TD
Conclusion:
In this tutorial, you learned how to print the initials of a name in Python. This can be done by getting the name from the user, splitting it into words, getting the first letter of each word, and then printing the initials.
This operation can come in handy when you need to print the initials of a large number of names at once.