In Python or any other programming language, Developers have to deal with a large amount of data most of the time and to handle such data they need to pre-process data. Creating or removing new lines from the data is a part of processing. To create a new line in python, we use the “/n” character most of the time. However, numerous Python functions automatically generate new lines. Therefore, they don’t even need to use “/n”.
Sometimes we do not need any newline, and if “/n” is used, we must remove the newline character. This write-up will discuss different functions to remove a new line from a string in Python.
Method 1: How to Remove Newline From String Using strip() Function?
The “strip()” function is used to remove the newline from a string in python. This function will remove both the trailing and leading newline of a string. A trailing line is whitespace that starts after the last character of the line and goes until the start of the new line, whereas the leading line is the whitespace that occurs before the start of the new line.
Let’s understand its working via the following example code:
Code
# Strip Function Example to remove new line from string using strip
string_value = "\n \n \nWelcome and Good Morning. \nHow are you?. \nwhat are you doing?...\n \n \n"
# Now we print the original string
print(string_value)
# Using Strip function to Remove Both trailing and leading newlines from string
updated_string = string_value.strip()
# Print updated string
print(updated_string)
In the above code:
- We are initializing the string with the newline character “/n” before starting the string and at the end of the string.
- In the second step, the original string is printed.
- Lastly, the strip() function is utilized to remove both the trailing and leading newlines of the string.
Output
The output shows that the “string()” function has removed all the trailing and leading whitespaces.
Note: If we want to remove only one trailing or leading line, we use this function with some modifications like the “rstrip()” and “lstrip()” functions.
Method 2: How to Remove Newline From String Using rstrp() Function?
The “rstrip()” function is used when we want to remove only the trailing line of a string. We use this function when we want whitespace between the end of the line and the start of the newline following example code makes use of the “rstrip()” function to remove the newline from a string in Python:
Code
# Strip Function Example to remove trailing line from string using rstrip function
string_value = "\n \n \nWelcome and Good Morning. \nHow are you?. \nwhat are you doing?...\n \n \n"
# Now we print the original string
print(string_value)
# Remove trailing newlines from string using r.strip function
updated_string_trl = string_value.rstrip()
# Print updated string
print(updated_string_trl)
In the above code:
- Strings value is initialized in the variable name “string_value”
- We used the “rstrip()” function to remove the trailing lines of our input string named “string_value”.
- Lastly, we print the output of the updated string value.
Output
In the above output, we have removed the trailing lines of our string using rstrip() function.
Method 3: How to Remove Newline From String Using lstrip() Function?
To remove the leading lines of a string, we use the “lstrip()” function. This function is utilized to remove a new line before the start of the string. Let’s understand it via the following example code:
Code
# Strip Function Example to remove leading line from string using lstrip function
string_value = "\n \n \nWelcome and Good Morning. \nHow are you?. \nwhat are you doing?...\n \n \n"
# Now we print the original string
print(string_value)
# Remove leading newlines from string using lstrip function
updated_string_ldg = string_value.lstrip()
# Print updated string
print(updated_string_ldg)
In the above code:
- String values are initialized in the variable named “string_value”.
- We have used the “lstrip()” function to remove the leading lines of our input string named “string_value”.
- Lastly, we have printed the output of updated string value without having leading lines.
Output
From the above output, it is noticed that there are no leading lines in the updated string value.
Method 4: How to Remove Newline From String Using replace() Function?
In Python, a built-in function named “replace()” is used to do various tasks like replacing substring,s, etc. This function can replace or remove the new line from the string. The following example code is practiced to remove all the new line values using this function:
Code
# using replace function we are removing new line
string_value2 = "Hello and welcome to my tutorial. \n\n\nThis is the discussion about String Newline."
# printing the original value of string with new line
print(string_value2)
# Now using function to Remove newlines between text
new_string = string_value2.replace("\n", "")
# printing new value of string
print(new_string)
In the above code:
- We used the newline character “/n” in between the string values.
- The “replace()” function has two arguments: “old value” and “new value”. We defined “/n” as an old value and did not define a new value because we only wanted to remove it. However, to replace the old value of the string then, we need to define the new value.
- The print() function is used to print the result.
Output
In the above output, we can see that the original string has two lines of whitespace between them, and all the new lines of the original string are removed.
Conclusion
In Python, a few built-in functions like “strip()”, “rstrip()”, “lstrip()” and “replace()” are used to remove the new line from a string. The “strip()” function alongside its modified functions i.e., “rstrip()” for trailing and “lstrip()” for leading, are used mostly. The replace() function is also used to replace or remove the new line from the string. In this post, all the above-stated functions are explained briefly to remove the newline from a substring.
TUTORIALS ON LINUX, PROGRAMMING & TECHNOLOGY