A newline separates one line of text from the next. In Python, it is represented as \n and commonly appears when printing or writing text. section 5.2
Use newlines deliberately when moving text between programs, files, and terminal input:
- print() adds a newline by default, while write() writes exactly the supplied text, so write() requires an explicit \n when a line should end. section 5.2
- A newline counts as part of the string and contributes to the reported length or byte count. section 5.2
- Pressing Enter terminates command-line input; the operating system removes that newline before passing the argument to the program. section 5.5.2
- Preserve line structure with splitlines() when transforming multiline input, then reconstruct the output without merging lines. section 15.0 section 18.1.6
Write and inspect two lines
>>> out_fh.write('this is some text\n') 18 >>> print('this is some more text', file=out_fh) >>> out_fh.close() >>> open('out.txt').read() 'this is some text\nthis is some more text\n' The file contains two lines because the first write explicitly includes \n and print() supplies the second newline. section 5.2
def main():
args = get_args()
for line in args.text.splitlines():
for word in line.split():
# what goes here?
print(' '.join(line.split()))
See a line-by-line processing pattern that uses splitlines() before splitting each line into words. section 18.1.6





