Convert Text to List by Lines in Python
This Snippet is coded by a user inOnline python Compiler. You can see and run this code too.
زبان: python
This Python code demonstrates how to use the splitlines() method to convert a multiline string into a list of lines.
- The variable txt contains three lines of text.
- Calling txt.splitlines() splits the string at line breaks and returns a list where each line becomes a separate item.
- The result is printed using print().
This technique is useful for reading text files, processing multiline input, or any task requiring line-based separation. Unlike split(), which works with a specified delimiter, splitlines() automatically recognizes different newline characters.