Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Selected Reading
Python - end parameter in print()
The print() function in Python automatically adds a newline character (\n) at the end of each print statement. However, you can customize this behavior using the end parameter to specify different ending characters or strings.
Syntax
print(value1, value2, ..., end='character_or_string')
Default Behavior
By default, print() ends with a newline character ?
print("Welcome to")
print("Tutorialspoint")
Welcome to Tutorialspoint
Using Space as End Character
You can replace the newline with a space to print on the same line ?
print("Welcome to", end=' ')
print("Tutorialspoint")
Welcome to Tutorialspoint
Using Custom Characters
The end parameter accepts any string or character ?
print("emailid", end='@')
print("tutorialspoint.com")
print("Python", end=' >>> ')
print("Programming")
print("Item1", end=', ')
print("Item2", end=', ')
print("Item3")
emailid@tutorialspoint.com Python >>> Programming Item1, Item2, Item3
Using Empty String
Setting end='' removes any ending character ?
print("Hello", end='')
print("World")
print("!")
HelloWorld !
Common Use Cases
| End Parameter | Use Case | Example |
|---|---|---|
end=' ' |
Print on same line with space | Creating horizontal lists |
end='' |
No separation between prints | Building strings piece by piece |
end=', ' |
CSV-like formatting | Comma-separated values |
end='\t' |
Tab separation | Creating tabular output |
Conclusion
The end parameter in print() gives you control over output formatting. Use it to create custom separators, build strings across multiple print statements, or format data in specific ways.
Advertisements
