Syntax and Usage

Code Examples

Example 1

This is the simplest form of the input() function with no arguments. When you run this code, the program will wait silently for you to type something and press Enter. The cursor will blink on an empty line, indicating that the program is waiting for your input. After you type something and press Enter, your input is stored in the response variable, and then displayed back to you. This form is rarely used in practice since it doesn't tell the user what to enter.

# Input without any prompt
response = input()
print("You entered:", response)

Example 2

This example adds a prompt inside the parentheses of the input() function. When the program runs, it shows "What is your favorite food? " and waits for your input. The prompt helps the user understand what information they should provide. Notice the space at the end of the prompt string - this is a good practice as it separates the prompt from the user's input on the screen, making it more readable.

# Input with a basic prompt
favorite_food = input("What is your favorite food? ")
print("Ah, " + favorite_food + " sounds delicious!")

Example 3

This example shows how to collect multiple inputs and store them in different variables. The program first asks for a first name, then a last name, storing each in its own variable. Then it combines these strings with a space in between to create a full name. This demonstrates how you can work with multiple input values and combine them.

# Storing inputs in multiple variables
first_name = input("Enter your first name: ")
last_name = input("Enter your last name: ")
full_name = first_name + " " + last_name

print("Your full name is:", full_name)

Example 4

This example demonstrates how to create a more complex, multi-line prompt using triple quotes ("""). The prompt includes a question and a list of options. When the program runs, it displays all of these lines before waiting for input. Triple quotes allow you to create prompts that span multiple lines without having to use multiple print statements. This is useful when you need to provide more detailed instructions or options to the user.

# Using a multi-line prompt
response = input("""
Please rate our service (1-5):
1 = Very Poor
2 = Poor
3 = Average
4 = Good
5 = Excellent
Your rating: """)

print("Thank you for your rating of", response)

Example 5

This example shows how to include variables within your input prompts using f-strings (formatted string literals). The program keeps track of the current question number and total questions, incorporating these values into each prompt to create a "Question X/Y" format. This helps users track their progress through a series of questions. The question_number variable increases after each question, demonstrating how you can dynamically change prompts throughout your program.

# Using variables within prompts
question_number = 1
total_questions = 3

answer1 = input(f"Question {question_number}/{total_questions}: What is your name? ")
question_number += 1

answer2 = input(f"Question {question_number}/{total_questions}: Where do you live? ")
question_number += 1

answer3 = input(f"Question {question_number}/{total_questions}: What is your favorite color? ")

print("\nThank you for completing the survey!")