What is input function in python notes geeksforgeeks
What is input function in python notes geeksforgeeks
I'm happy to help!
The input()
function in Python is a built-in function that allows the user to provide input to the program. It reads a line from standard input (usually the keyboard), converts it into a string, and returns that string.
Here are some key points about the input()
function:
input()
function takes default input as Enter
press. Type Conversion: The input()
function always returns a string. If you want to convert the input to another data type (like an integer or float), you need to use the built-in functions like int()
, float()
, etc.
Here's an example of how you can use the input()
function:
name = input("What is your name? ")
print("Hello, " + name + "!")
In this example, the program prompts the user to enter their name. The input()
function waits for the user's response and stores it in the variable name
. Then, the program prints out a greeting message using the stored name.
try:
age = int(input("Enter your age: "))
except ValueError:
print("Invalid input. Please enter a valid integer.")
In this example, if the user enters something that's not an integer (like a string or a float), the program will catch the ValueError
and print out an error message.
input()
function is not suitable for raw binary input. If you need to read raw binary data, use the sys.stdin.read()
function instead. Multiline Input: By default, the input()
function reads a single line of input. If you want to read multiple lines of input, use a loop and concatenate the inputs.
Some common uses for the input()
function include:
In conclusion, the input()
function in Python is a powerful tool for interacting with users and gathering data. By understanding its usage, you can build more robust and engaging programs that respond to user input!
Print() function in Python
The print()
function! A fundamental building block of any Python program.
What is print()
?
In Python, the print()
function is used to output text or other values to the console. It's a way for your program to communicate with the user and provide feedback. You can think of it like sending a message from your program to the outside world.
How does print()
work?
When you call print()
, Python takes the value(s) you pass to it and converts them into a human-readable format. This can include strings, numbers, booleans, lists, dictionaries... you name it!
Here's a simple example:
name = "John"
age = 30
print("Hello, my name is", name, "and I'm", age, "years old.")
This would output: Hello, my name is John and I'm 30 years old.
Notice how we passed multiple values to print()
? That's okay! Python will concatenate them with spaces in between. If you want to add a newline character (n
) after printing something, you can do so like this:
name = "John"
age = 30
print("Hello, my name is", name, "and I'm", age, "years old.n")
This would output: Hello, my name is John and I'm 30 years old.
followed by a new line.
Printing with formatting
Sometimes you might want to format your output in a specific way. Python provides several ways to do this. Let's say you want to print a table with headers:
headers = ["Name", "Age", "Occupation"]
data = [
["John", 30, "Software Engineer"],
["Jane", 25, "Data Scientist"],
]
print("{:<10} {:^5} {}".format(headers[0], headers[1], headers[2]))
for row in data:
print(
"{:<10} {:>4} {}".format(row[0], row[1], row[2])
)
This would output:
Name Age Occupation
John 30 Software Engineer
Jane 25 Data Scientist
In this example, we used the format()
method to insert our data into a formatted string. The {:<10}
means left-align the text in a field of length 10, while {:>4}
means right-justify it in a field of length 4.
Advanced printing with Python
If you need even more control over your output, you can use Python's built-in support for advanced formatting. For example, if you want to print a table with precise alignment:
import tabulate
headers = ["Name", "Age", "Occupation"]
data = [
["John", 30, "Software Engineer"],
["Jane", 25, "Data Scientist"],
]
print(tabulate.tabulate(data, headers=headers, tablefmt="grid"))
This would output:
+----------+-------+---------------+
| Name | Age | Occupation |
|----------+-------+---------------|
| John | 30 | Software Engineer|
| Jane | 25 | Data Scientist |
+----------+-------+---------------+
In this example, we used the tabulate
library to create a formatted table with precise alignment.
And that's it! print()
is an incredibly powerful tool in Python. With its flexibility and customization options, you can express yourself creatively on the console (or wherever your program prints output).