In this tutorial, you will learn how to input a name using Python programming language. This is a fundamental task that will help you better understand how to interact with users and collect data from them.
By the end of the tutorial, you’ll be able to create a simple program that takes a user’s name as input and has some fun with it. Let’s get started!
Step 1: Get User Input
First, we need to get the user’s name as input. To do this, we will use the input() function. The input() function displays a prompt to the user and waits for them to enter some text. The text entered by the user can then be stored in a variable.
Here’s how to use the input function:
1 |
name = input("Please enter your name: ") |
When the user runs this code, they will see the prompt “Please enter your name: ” and can type their name.
Step 2: Process the Input
Now that we have the user’s name, we can do something with it. For example, let’s greet them by their name:
1 |
print("Hello, " + name + "!") |
This code combines the string “Hello, ” with the user’s name and an exclamation mark using the “+” operator, and then prints the result.
Step 3: Add Some Fun
Now let’s add some fun to our program. We can manipulate the user’s name in various ways. For instance, we can print their name in reverse order:
1 |
print("Your name reversed is: " + name[::-1]) |
The code name[::-1] takes the name variable and reverses it by slicing it with a step of -1.
Full Code
Now let’s put all the steps together into a complete program.
1 2 3 |
name = input("Please enter your name: ") print("Hello, " + name + "!") print("Your name reversed is: " + name[::-1]) |
Output
When the user runs the code, the output will look like this (assuming the user inputs “Tom”):
Please enter your name: Tom Hello, Tom! Your name reversed is: moT
Conclusion
Congratulations! You have now learned the basics of getting user input in Python and created a fun program that takes a name as input and reverses it. This simple task introduces you to the input() function, string manipulation, and printing results. As you continue learning Python, you’ll find more ways to interact with users and process their data.
Feel free to experiment with the code and try adding new features, such as counting the number of characters, vowels, or consonants in the name. Happy coding!