Day1: Learning to Use the input() Function in Python to Collect User Input

Day1: Learning to Use the input() Function in Python to Collect User Input

How to use Python’s input() function to collect data from a user and integrate it into my code. It’s a super useful function when you need interactive programs!

?? PAUSE 1: Using input() to Collect User Input and Add an Exclamation Mark

We can use the input() function to ask the user for their name, then combine it with other strings using string concatenation to form a complete sentence.

Here’s how you can do it:

# Collecting user input
name = input("What is your name? ")

# Concatenating the input with other strings and adding an exclamation mark
greeting = "Hello " + name + "!"

# Printing the result
print(greeting)        

?? Explanation:

  • input("What is your name? "): This prompts the user to type in their name and stores it in the variable name.
  • String Concatenation: We concatenate the string "Hello " with the user’s input (stored in name) and add an exclamation mark "!".
  • Output: The final output is the sentence "Hello [Name]!".

???? Example Output:

If the user enters "Hanzalah", the output will be:

What is your name? Hanzalah
Hello Hanzalah!        

This is a simple but powerful way to personalize your program's output based on user input. It’s also a great stepping stone for more complex programs that require user interaction!

Stay tuned as I continue my Python journey and explore more coding skills during my #100DaysOfCode! ??

要查看或添加评论,请登录

Muhammad Hanzala的更多文章

社区洞察

其他会员也浏览了