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

Publicado em: 07 Março 2025
no canal de: 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?


Nesta página do site você pode assistir ao vídeo on-line Python | print objects (Format Parameter) Program | Practical & theory duração hora minuto segundo em boa qualidade , que foi baixado pelo usuário PassTech 07 Março 2025, compartilhe o link com seus amigos e conhecidos, no youtube este vídeo já foi visto 9 vezes e gostou 2 espectadores. Boa visualização!