Python | print objects (Format Parameter) Program | Practical & theory

Published: 07 March 2025
on channel: PassTech
9
2

In Python, the `print()` function allows you to display objects, and you can customize the format of the output using the `sep`, `end`, and `format` parameters.

Here's a brief explanation of each:

1. `sep` (separator):
It defines what separates the printed objects. By default, it is a space (`' '`).
You can change it to any string or character to separate your printed values.

Example:
```python
print("Hello", "world", sep="-") # Output: Hello-world
```

2. `end`:
It defines what to append at the end of the print output. By default, it is a newline (`\n`).
You can change it to another string or even remove the newline entirely.

Example:
```python
print("Hello", end=" ")
print("world") # Output: Hello world
```

3. `format` (string formatting):
You can format the output using f-strings, `.format()`, or `%` formatting.

#### Example 1: Using f-strings (Python 3.6+)
```python
name = "Alice"
age = 30
print(f"My name is {name} and I am {age} years old.") # Output: My name is Alice and I am 30 years old.
```

#### Example 2: Using `.format()`
```python
name = "Alice"
age = 30
print("My name is {} and I am {} years old.".format(name, age)) # Output: My name is Alice and I am 30 years old.
```

4. Printing Multiple Objects with Custom Format:
You can also use `print()` with multiple arguments and customize the formatting of each one:

```python
name = "Alice"
age = 30
height = 5.6
print(f"Name: {name}, Age: {age}, Height: {height}m")
```

Example with `sep` and `end`:
```python
print("Hello", "world", sep="-", end="!") # Output: Hello-world!
```

Would you like to see an example of formatting objects in a more complex way?


On this page of the site you can watch the video online Python | print objects (Format Parameter) Program | Practical & theory with a duration of hours minute second in good quality, which was uploaded by the user PassTech 07 March 2025, share the link with friends and acquaintances, this video has already been watched 9 times on youtube and it was liked by 2 viewers. Enjoy your viewing!