How to do add newline character in Python

Python PythonBasic

In this post, we are going to explore 8 different ways to How to do add newline character in Python that includes Python add newline in code, Python adds a newline to string, Python newline after the variable, Newline in Python print statement with code examples

1. Add newline character to a Python List


In this example, we will learn how to add a newline character to a Python List.

To add a newline character between each element of the list, The string’s join() function is used. Then we will print each element of the list in a new line.

original_list = ['apple','banana','Gauava','Grapes','kiwi']

print(f"original list without new line charcater:",original_list)

result_list = '\n'.join(original_list)

print(f"\n result list after adding newline character:\n{result_list}")

Output

original List without new line charcater:
['apple', 'banana', 'Gauava']

Result list after adding newline character:
apple
banana
Gauava

2. Newline character in Python print statement


The Python print() line statement adds a new line character(\n) at the end of the string by default.

In this example output is printing in separate two lines, because at the end of each line print statement adds a new line character.

greet = "hello"
Message = "how are you"
print(greet)
print(Message)

Output

hello
how are you

3. How to print without new line?


As you saw the default behavior of the print() function in Python.In Python 3+ version new parameter end= was added to print() function.Let us see with an example How to print without a newline in the Print() function.

We have to split the string by using end=” ” space delimiter as the second parameter.The delimiter can be (‘,’,’ ‘,’-‘,’\n’,’/’) and many more.

greet = "hello,"
Message = "how are you"
print(greet,end=" ")
print(Message)

Output

hello, how are you

4. Add a line break in a print statement


To add a line break in the Python print() function we can use the newline character at the end of each string.

print("hello,\nhow are you\nwhat's up\nWelcome to devenum")

Output

hello,
how are you
what's up
Welcome to devenum

5. How to Add new line character using f-string?


The Python f-string is also known as a formatted string allows greater control of how value is formatted. Let us understand how to add a new line character to the f-string.

newline = "\n"
mystring = f"hello,how are you{newline}Welcome to devenum{newline}Good morning"
print(mystring)

output

hello,how are you
Welcome to devenum
Good morning

6. How to write newline to a file?


To append a new line character at the end of each line of the file, We can pass a new line character to the write() method of the file.

write("writing text to file \n")

7. Python add a newline to string


In Python, Multiline strings are represented by Using triple-double quotes ” ” ” or single quotes(‘ ‘ ‘) at the start and end of the string.

The newline character can be added between these triple quotes.

Program to Add newline charcater To Multiline string

Welcome_note = """Welcome to devenum,How are you,\ngood to see you again.\nThanks for visiting us again.\n"""
print(Welcome_note )

Output

Welcome to devenum,How are you,
good to see you again.
Thanks for visiting us again.

Program to add new line character to string

Welcome_note = "Welcome to devenum\n\
How are you,\n\
good to see you again.\n\
Thanks for visiting us again.\n"
print(Welcome_note )

Output

Welcome to devenum
How are you,
good to see you again.
Thanks for visiting us again.

Summary

In this post, we have learned about the Python newline character in code examples like how to Add newline character to a Python List, Newline character in Python print statement, How to print without a newline, Add a line break in a print statement, How to Add new line character using f-string, How to write newline to a file, Python add a newline to string

Reference : Quora