Python Input and Output Functions
๐ Python Input and Output Functions
Python provides built-in functions to take input from the user and display output on the screen.
๐น Input Function: input()
The input() function is used to get user input as a string.
๐ง Syntax:
python
Copy
Edit
variable = input("Enter something: ")
✅ Example:
python
Copy
Edit
name = input("What is your name? ")
print("Hello, " + name + "!")
๐ Note: The input is always stored as a string, even if you enter numbers. To convert it, use functions like int() or float():
python
Copy
Edit
age = int(input("Enter your age: "))
๐น Output Function: print()
The print() function is used to display output on the screen.
๐ง Syntax:
python
Copy
Edit
print(object1, object2, ..., sep=' ', end='\n')
✅ Examples:
python
Copy
Edit
print("Hello, world!")
You can print multiple items:
python
Copy
Edit
print("Name:", "Alice")
Use sep to change the separator between items:
python
Copy
Edit
print("2025", "04", "12", sep="-")
# Output: 2025-04-12
Use end to change what’s printed at the end:
python
Copy
Edit
print("Hello", end=" ")
print("World")
# Output: Hello World
๐งช Example Program:
python
Copy
Edit
name = input("Enter your name: ")
age = int(input("Enter your age: "))
print("Hi", name + "!", "You are", age, "years old.")
Let me know if you'd like examples for file input/output (open(), read(), write()) too!
Learn Python Course in Hyderabad
Read More
Python Comments and Their Importance
Where can I find good courses for Python?
Visit Our Quality Thought Training in Hyderabad
Comments
Post a Comment