The newline(\n) or line break is required to add different parts of the python script, such as inside the string, list or dictionary or tuple items, etc. Different ways to print the newline(\n) in Python script have shown in this tutorial.
Use of newline in Python script:
Adding newline in different parts of the python script has been shown in this tutorial.
Example-1: Print the newline inside the string values
Adding a new line inside the string value is a very common task of the python script. Create a python file with the following script to know how to add newline in different parts of the string value. In the string1 variable, the newline character (\n) has used in the middle of the single-quoted string value. In the string2 variable, the newline character (\n) has used two times in the middle of the double-quoted string value. In the string3 variable, the newline character (\n) has used in the middle of the triple single-quoted string value. In the string4 variable, the newline character (\n) has used in the middle of the triple double-quoted string value. In the string5 variable, the variable containing newline character (\n) has been used inside the formatted string.
string1 = 'Welcome to\n Linuxhint'
print("The first newline output:\n", string1)
# Define string with double quotes
string2 = "Python\n Bash\n Perl"
print("\nThe second newline output:\n", string2)
# Define string with three single quotes
string3 = '''Python is a \n interpreted language.'''
print("\nThe third newline output:\n", string3)
# Define string with three double quotes without newline(\n)
string4 = """I
like
Python
programming"""
print("\nThe fourth newline output:\n", string4)
# Assign newline(\n) character into a variable
nl = '\n'
# Use the variable in the string
string5 = f"Learn{nl} Python."
print("\nThe fourth newline output:\n", string5)
Output:
The following output will appear after executing the above script.
Example-2: Print the newline inside the list items
Create a python file with the following script to print each list value in a line by using for loop and joining the list items with the newline character (\n). A list of three elements has been declared in the script, and the values of this list have been printed by using for loop and the join() function.
employee_list = ['Nuruzzaman', 'Md. Arafat', 'Abir Chowdhury']
# Print the list items
print("The original list values:\n", employee_list)
# Print each list item in a line
print("\nThe list values by using loop:")
for value in employee_list:
print(value)
# Create string by joining list items with newline
output = '\n '.join(employee_list)
print("\nThe list values with newline by using join() function:\n", output)
Output:
The following output will appear after executing the above script.
Example-3: Print the newline inside the dictionary items
Create a python file with the following script to print each key and value of a dictionary in a line by using for loop. Next, each key and value of the dictionary has been printed in a line separately by using the join() function.
dic_students = {"ID": "09785", "Name": "Kamrul Hasan",
"Batch": "10", "Semester": "6"}
# Print the dictionary items
print("The original dictionary items:\n", dic_students)
# Print each key and value of the dictionary with line break
print("\nThe dictionary keys and values with newline:")
for key, value in dic_students.items():
print("{} : {}".format(key, value))
# Create string by joining dictionary keys with newline
output = '\n '.join(dic_students.keys())
print("\nThe dictionary keys with newline by using join() function:\n", output)
# Create string by joining dictionary values with newline
output = '\n '.join(dic_students.values())
print("\nThe dictionary values with newline by using join() function:\n", output)
Output:
The following output will appear after executing the above script.
Conclusion:
The ways of adding newline (\n) inside the string, list, and dictionary variables have been shown in this tutorial using various examples.