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

Publié le: 07 mars 2025
sur la chaîne: 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?


Sur cette page du site, vous pouvez voir la vidéo en ligne Python | print objects (Format Parameter) Program | Practical & theory durée heure minute seconde en bonne qualité , qui a été Téléchargé par l'utilisateur PassTech 07 mars 2025, Partagez le lien avec vos amis et connaissances, sur youtube cette vidéo a déjà été regardée 9 fois et il a aimé 2 téléspectateurs. Bon visionnage!