w3resource

Python: Remove a newline in Python

Python String: Exercise-23 with Solution

Write a Python program to remove a newline in Python.

Python String Exercises: Remove a newline in Python

Sample Solution:

Python Code:

# Define a variable 'str1' and assign it the value 'Python Exercises' followed by a newline character.
str1 = 'Python Exercises\n'

# Print the value of 'str1', which includes a newline character, resulting in a new line in the output.
print(str1)

# Use the rstrip() method to remove trailing whitespace characters, including the newline character.
# Then, print the modified 'str1' with trailing whitespace removed.
print(str1.rstrip()) 

Sample Output:

Python Exercises                                                                                              
                                                                                                              
Python Exercises 

Python Code Editor:

Previous: Write a Python program to sort a string lexicographically.
Next: Write a Python program to check whether a string starts with specified characters.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Become a Patron!

Follow us on Facebook and Twitter for latest update.

It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.

https://198.211.115.131/python-exercises/string/python-data-type-string-exercise-23.php