> For the complete documentation index, see [llms.txt](https://python-codelivly.gitbook.io/python-mastery-from-beginner-to-expert/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://python-codelivly.gitbook.io/python-mastery-from-beginner-to-expert/basics-of-python-programming/input-and-output.md).

# Input and Output

In Python, you can take input from the user using the `input()` function and output data using the `print()` function. These are the two most commonly used functions for basic input and output operations in Python.&#x20;

## Python input()

\
`input()` function is a built-in function in Python that reads a line from the user and returns it as a string.

### **Syntax:**

```css
input([prompt])
```

The `input()` function takes one optional parameter `prompt`, which is a string that is displayed to the user before they enter the input.

### **Parameters:**

* `prompt` (optional): A string that is displayed to the user before they enter the input. If this parameter is not provided, the function waits for the user to input any value.

### **Return Value:**

The `input()` function returns the user input as a string.

Here is an example of using the `input()` function:

```python
name = input("What is your name? ")
print("Hello, " + name + "!")
```

In this example, the `input()` function prompts the user to enter their name, which is stored in the `name` variable. The `print()` function then outputs a personalized greeting to the user.

Note that the value returned by `input()` is always a string, so if you need to perform numeric operations on the input, you must convert it to the appropriate data type (e.g. using the `int()` or `float()` functions).

```python
age = int(input("What is your age? "))
print("You will be " + str(age + 10) + " in 10 years.")
```

In this example, the `input()` function prompts the user to enter their age, which is converted to an integer using the `int()` function. The program then adds 10 to the user's age and prints the result as a string using the `str()` function.&#x20;

### Example 1: Python Print Statement <a href="#example1" id="example1"></a>

Here's an example of the Python `print()` statement:

```python
# Simple print statement
print("Hello, World!")

# Print with a variable
message = "Welcome to Python Programming!"
print(message)

# Print with multiple values
name = "John"
age = 25
print("My name is", name, "and I am", age, "years old.")

```

Output:

```css
Hello, World!
Welcome to Python Programming!
My name is John and I am 25 years old.
```

## Python Output&#x20;

In Python, the `print()` function is used to output values to the console. The `print()` function can take one or more values separated by commas as arguments, which it then outputs to the console.

Here's an example:

```bash
print("Hello, world!")
```

This code will output `Hello, world!` to the console. You can also pass in multiple values to `print()` by separating them with commas:

```makefile
name = "Alice"
age = 25
print("My name is", name, "and I am", age, "years old.")
```

This code will output `My name is Alice and I am 25 years old.` to the console.

You can also use escape sequences to format the output. For example, to output a newline character, use the  escape sequence:

```swift
print("First line\nSecond line")
```

This code will output:

```arduino
First line
Second line
```

There are many other escape sequences you can use, such as  for a tab character and `\\` to output a backslash. You can also use string formatting to format the output in a more structured way.

### **Syntax of print()**&#x20;

The syntax of the `print()` function in Python is:

```python
print(*objects, sep=' ', end='\n', file=sys.stdout, flush=False)
```

* `objects`: zero or more objects to be printed.
* `sep`: separator between the objects. It is optional, and the default value is a single space character `' '`.
* `end`: end string after the last object is printed. It is also optional, and the default value is a newline character `'\n'`.
* `file`: the object where the printed output will be sent. It is optional, and the default value is the standard output `sys.stdout`.
* `flush`: a boolean value indicating whether to flush the stream. It is optional, and the default value is `False`.

The `print()` function can take multiple arguments separated by a comma and print them one after the other. The values can be of any data type, including strings, numbers, and variables.

For example:

<pre class="language-python"><code class="lang-python"><strong>print("Hello, World!")
</strong>print(42)
print("My name is", "John", "Doe")
</code></pre>

Output:

```csharp
Hello, World!
42
My name is John Doe
```

### Output formatting

Output formatting refers to how the output of a program is presented to the user. In Python, the built-in `print()` function allows you to format the output in various ways.

One way to format output is by using string formatting. Python provides a number of ways to format strings, such as using the `%` operator, the `format()` method, and f-strings.

Another way to format output is by using escape characters. Escape characters are special characters that allow you to include non-printable characters in your output, such as newlines, tabs, and backslashes.

Here's an example of using string formatting with the `%` operator:

```python
name = "John"
age = 30
print("My name is %s and I am %d years old." % (name, age))
```

Output:

```csharp
My name is John and I am 30 years old.
```

And here's an example of using f-strings:

```python
name = "John"
age = 30
print(f"My name is {name} and I am {age} years old.")
```

Output:

```csharp
My name is John and I am 30 years old.
```

You can also use escape characters to format your output. For example, you can use  to insert a newline character,  to insert a tab character, and `\\` to insert a backslash:

```python
print("First line\nSecond line")
print("Tabbed\toutput")
print("Backslash \\")
```

Output:

```arduino
First line
Second line
Tabbed    output
Backslash \
```

Output formatting can make your program's output more readable and easier to understand, especially when dealing with large amounts of data.\ <br>
