Python Forum
problem with spliting line in print
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
problem with spliting line in print
#1
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
Reply
#2
But not lengthy strings. Using \ to make long lines should be avoided in any case.
Reply
#3
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}")
akbarza likes this post
Reply
#4
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.
akbarza likes this post
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  problem with print command in super() akbarza 5 605 Feb-01-2024, 12:25 PM
Last Post: deanhystad
  problem with print lists MarekGwozdz 4 697 Dec-15-2023, 09:13 AM
Last Post: Pedroski55
  Problem with print variable in print.cell (fpdf) muconi 0 668 Dec-25-2022, 02:24 PM
Last Post: muconi
  Print the line before the corrent line tester_V 9 1,578 Nov-18-2022, 08:39 AM
Last Post: Gribouillis
  Print to a New Line when Appending File DaveG 0 1,229 Mar-30-2022, 04:14 AM
Last Post: DaveG
  If match not found print last line tester_V 2 2,897 Apr-26-2021, 05:18 AM
Last Post: tester_V
  print a line break in writelines() method leodavinci1990 1 6,481 Oct-12-2020, 06:36 AM
Last Post: DeaD_EyE
  Print characters in a single line rather than one at a time hhydration 1 2,043 Oct-10-2020, 10:00 PM
Last Post: bowlofred
  How to print string multiple times on new line ace19887 7 5,781 Sep-30-2020, 02:53 PM
Last Post: buran
  Pattern Require Last Line Print() why? Harshil 4 2,446 Aug-08-2020, 04:54 PM
Last Post: Harshil

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020