Python strip with \n [duplicate]

You should be able to use line.strip('\n') and line.strip('\t'). But these don’t modify the line variable…they just return the string with the \n and \t stripped. So you’ll have to do something like

line = line.strip('\n')
line = line.strip('\t')

That should work for removing from the start and end. If you have \n and \t in the middle of the string, you need to do

line = line.replace('\n','')
line = line.replace('\t','')

to replace the \n and \t with nothingness.

Leave a Comment