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?
Auf dieser Seite können Sie das Online-Video Python | print objects (Format Parameter) Program | Practical & theory mit der Dauer stunde minuten sekunde in guter Qualität ansehen, das der Benutzer PassTech 07 März 2025 hochgeladen hat, den Link mit Freunden und Bekannten teilen, dieses Video wurde auf Youtube bereits 9 Mal angesehen und es wurde von 2 den Zuschauern gefallen. Viel Spaß beim Betrachtenden Zuschauern gefallen!