How to Fold a Long Single Line Text to Multiple Lines (in Python and Linux)?

You might want to convert a really long string in a single line into multiple lines with specified width. For example, if you have string like


and want to split the long text to multi-line text, where each line is of length w, like w=9


In the above example, each line has been formatted to have width of w=9 and the last line has the remaining text. One immediate application of a solution to this problem is in formating a chromosome long genome sequence into multi-line output format like FASTA.

Here are two ways to fold a long line; one using a few lines of Python code, and the other using a simple Linux command on terminal.

Python Code to Convert a Long Single Line to Multi-Line Text

def fold_W(line, w, ofile):
    Fold a long line to multiple lines
    with width w (int)
    Input: a long line of text
    Input: line width
    Output: file with multi-line output
    out = open(ofile, "w")
    line = line.rstrip()
    # get the length of long line
    line_length = len(line)
    # loop through the long line using the desired line
    # width
    while line_length - start >= w:
          print >>out, line[start:start+w]
          start += w
    # The rest of the line that does not completely
    # use a line of length w
    print >>out, line[start:]

Linux Command to Convert a Long Single Line to Multi-Line Text

Although the above multi-line code to convert a single line code works fine, there is elegant solution from a Linux command. The Linux command that does the job neatly in a single line is “fold”. If the long line is in a file named “longLine.txt”, one can split into multi-line text by using “fold” command as

fold -w 9 longLine.txt

where the -w option specifies the line width for the multi-line output.