Python Forum

Full Version: problem with spliting line in print
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
hi
excuse me for this silly question, but i have problem with it. in below code :
"""
shallowcopy and deepcopy
from: https://realpython.com/python-mutable-vs  \
-immutable-types/#common-mutability-related-gotchas
"""
import copy

matrix = [[1, 2, 3], [4, 5, 6], [6, 7, 8]]

shallow_copy = copy.copy(matrix)  # Same as: matrix.copy() or matrix[:]
deep_copy = copy.deepcopy(matrix)
print(f"matrix is: {matrix}")
print(f"id(matrix) is: {id(matrix)}")
print(f"shallow_copy = copy.copy(matrix) , \
deep_copy = copy.deepcopy(matrix)") 
print(f"id(shallow_copy) is {id(shallow_copy)}")
print(f"id(deep_copy) is {id(deep_copy)}")
print(f"result of id(matrix[0]) == id(shallow_copy[0]) is : \ 
{id(matrix[0]) == id(shallow_copy[0]}")
print(f"result of id(matrix[0]) == id(deep_copy[0]) is: {  \
id(matrix[0]) == id(deep_copy[0])}")
matrix[1][1] = 555
print(f"matrix[1][1] is set to 555")
print(f"matrix is now: {matrix}")
print(f"shallow_copy is now: {shallow_copy}")
print(f"depp_copy is now: {deep_copy}")
when I run it, I get the below:
Error:
>>> %Run shallow_deep_copy_example1.py Traceback (most recent call last): File "D:\akb_python\akb_py_projects\shallow_deep_copy_example1.py", line 18 print(f"result of id(matrix[0]) == id(shallow_copy[0]) is : \ ^ SyntaxError: unterminated string literal (detected at line 18) >>>
I read on the net that \ is used for breaking lengthy lines, so I used it for breaking lines, but I encountered an error.
how can I break the length line in the print command?
thanks
But not lengthy strings. Using \ to make long lines should be avoided in any case.
Why are you breaking lines like this?

You have a problem in line 19, you have to close the parentesis on the id(shallow_copy[0])} function:

You don't need slashes when breaking f-strings, but you must start and end them properly:
import copy

matrix = [[1, 2, 3], [4, 5, 6], [6, 7, 8]]

shallow_copy = copy.copy(matrix)  # Same as: matrix.copy() or matrix[:]
deep_copy = copy.deepcopy(matrix)
print(f"matrix is: {matrix}")
print(f"id(matrix) is: {id(matrix)}")
print(f"shallow_copy = copy.copy(matrix), "
      f"deep_copy = copy.deepcopy(matrix)")
print(f"id(shallow_copy) is {id(shallow_copy)}")
print(f"id(deep_copy) is {id(deep_copy)}")
print(f"result of id(matrix[0]) == id(shallow_copy[0]) is : "
      f"{id(matrix[0]) == id(shallow_copy[0])}")
print(f"result of id(matrix[0]) == id(deep_copy[0]) is: "
      f"{id(matrix[0]) == id(deep_copy[0])}")
matrix[1][1] = 555
print(f"matrix[1][1] is set to 555")
print(f"matrix is now: {matrix}")
print(f"shallow_copy is now: {shallow_copy}")
print(f"depp_copy is now: {deep_copy}")
Notice there is no comma between the strings in carecavoador's response:
print(f"shallow_copy = copy.copy(matrix), "
      f"deep_copy = copy.deepcopy(matrix)")
The two strings are concatenated and printed as one string.