In Python 2.4, how can I strip out characters after ';'? In Python 2.4, how can I strip out characters after ';'? python python

In Python 2.4, how can I strip out characters after ';'?


I'd recommend saying

line.split(";")[0]

which will give you a string of all characters up to but not including the first ";" character. If no ";" character is present, then it will give you the entire line.


just do a split on the line by comment then get the first elementeg

line.split(";")[0]


For Python 2.5 or greater, I would use the partition method:

rtr = line.partition(';')[0].rstrip() + '\n'