Remove line breaks from strings in python, and wrap the lines in python.

Source: Internet
Author: User

Remove line breaks from strings in python, and wrap the lines in python.

This is written today. Use python to remove linefeeds from strings and write them into files. Check the code online.Replace ("\ n ",""),After that, it took half a day, but it was still wrong.

The above are the problems I encountered today. The following are the solutions.

The local test is a Windows system, which is officially used on unix servers. I am not very clear about the specific differences between the two line breaks. Therefore, the file written into the string is opened with notepad ++, and the line tail character is displayed (how to use notepad ++ to display the line tail character Baidu). The result is CR, so change the code to replace ("\ r", "") to perfect solution.

Summary:

If the end of a line is CR, replace ("\ r", "") is used ","")

If the line end character is LF, replace ("\ n", "") is used ","")

The following describes how to remove spaces and line breaks in python.

I. Remove Spaces

Strip ()

"Xyz ". strip () # returns "xyz" "xyz ". lstrip () # returns "xyz" "xyz ". rstrip () # returns "xyz" "x y z ". replace ('','') # returns "xyz"

2. replace ("space ","")

UseReplace ("\ n ",""),Replace the trailing string with the trailing string

Summary

The above section describes how to remove line breaks from strings in python. I hope it will be helpful to you. If you have any questions, please leave a message and I will reply to you in a timely manner. Thank you very much for your support for the help House website!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.