Python input() Built in Function
The input() function allows user input from the standard input device (usually keyboard). It takes an optional prompt parameter that displays a message before taking the input. The input() function returns a string value that can be assigned to a variable or used directly.
Here are some examples of using the input() function:
# Example 1: Input with prompt
name = input("Enter your name: ") # Enter your name: John
print("Hello, " + name) # Hello, John
# Example 2: Input without prompt
color = input() # Red
print("Your favorite color is " + color) # Your favorite color is Red
# Example 3: Input with Unicode characters
emoji = input("Enter an emoji: ") # Enter an emoji: 😊
print("You entered " + emoji) # You entered 😊
# Example 4: Input a number and convert it to integer
age = int(input("Enter your age: ")) # Enter your age: 25
print("You are " + str(age) + " years old") # You are 25 years old
Here are some examples of using the input() function:
# Example 1: Input with prompt
name = input("Enter your name: ") # Enter your name: John
print("Hello, " + name) # Hello, John
# Example 2: Input without prompt
color = input() # Red
print("Your favorite color is " + color) # Your favorite color is Red
# Example 3: Input with Unicode characters
emoji = input("Enter an emoji: ") # Enter an emoji: 😊
print("You entered " + emoji) # You entered 😊
# Example 4: Input a number and convert it to integer
age = int(input("Enter your age: ")) # Enter your age: 25
print("You are " + str(age) + " years old") # You are 25 years old
If you have any questions about this code, you can drop a line in comment.
Comments
Post a Comment