In this tutorial we will learn about EOL in python with some cool and easy examples .In many situations you must have to come up with this type of requirements.
EOL In Python With Example
In Python, “EOL” typically refers to “End of Line,” which signifies the termination of a line in a text file or a string. Python provides multiple ways to represent the end of a line in strings and files, depending on the operating system.
- Using ‘\n’ (Newline character): This is the most common method to represent the end of a line in Python. It works on all major operating systems (Windows, Linux, macOS).
# Example of using '\n' to represent end of line text = "This is line 1.\n This is line 2.\n This is line 3." print(text)
Output:
This is line 1.
This is line 2.
This is line 3.
- Using ‘os.linesep’: This method uses the ‘
os'
module to get the appropriate end-of-line sequence for the current operating system.import os # Example of using os.linesep to represent end of line text = "This is line 1." + os.linesep + "This is line 2." + os.linesep + "This is line 3." print(text)
Output would be the same as the first example, but it’s platform-independent.
- Using Triple Quotes: Triple quotes can be used to create multi-line strings, where each new line is implicitly marked by a line break.
# Example of using triple quotes for multi-line strings text = """This is line 1. This is line 2. This is line 3.""" print(text)
Output is the same as the previous examples.