Python: String Operation

From OnnoWiki
Revision as of 06:13, 30 January 2017 by Onnowpurbo (talk | contribs) (Created page with " ==Strip /n== line.strip() will remove all types of whitespaces from both ends of the line. You can use line.rstrip("\n") to remove only the trailing "\n". ==Join Line==...")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search


Strip /n

line.strip() will remove all types of whitespaces from both ends of the line. You can use line.rstrip("\n") to remove only the trailing "\n".


Join Line

Pakai str.join:

with open('file.txt') as f:

   print " ".join(line.strip() for line in f)