+ 2

I don't understand why g1, g2 & g3 values is not aligned with Group 1, Group 2 and Group 3 which is the expected output..

players = ["Alice", "Bob", "Charlie", "David", "Eve", "Frank"] #Create 3 lists with 2 players each #Use slicing to create a list for Group 1 g1 = players [0:2] #Use slicing to create a list for Group 2 g2 = players [2:4] #Use slicing to create a list for Group 3 g3 = players [4:6] print("Group 1: \n",g1) #display the 1st group print("Group 2: \n",g2) #display the 2nd group print("Group 3: \n",g3) #display the 3rd group

6th May 2024, 2:14 PM
Mahmuda Akter Moon
6 odpowiedzi
+ 5
Moon , the reason for your issue is, that the default separator between 2 arguments in the print() function is a space. so the second line that outputs the list starts with a space. we can avoid this by: using 2 separate print statements, each for one line or: adding sep='\n' as a keyword argument in the print() function. `sep` defines the separator between the arguments in print function. in this case it is the newline sequence `\n`. or: using an f-string inside the print() function like: f'Group 1:\n{g3}'
6th May 2024, 3:05 PM
Lothar
Lothar - avatar
+ 2
Are you sure the output is exactly the same as expected output? From what I see, it is very close but no t the same.
6th May 2024, 2:53 PM
Wong Hei Ming
Wong Hei Ming - avatar
+ 2
List slicing returns a new list from the existing list.
6th May 2024, 2:54 PM
JaScript
JaScript - avatar
+ 1
Lothar Thank you so much.. I tried using f string it didn't work.. Then I tried separate print statements for each line. It worked..
6th May 2024, 3:16 PM
Mahmuda Akter Moon
+ 1
Moon Using f-string does work and I tested with that just now. However, it doesn't work if you put a space between colon : and \n
6th May 2024, 3:23 PM
Wong Hei Ming
Wong Hei Ming - avatar
0
Wong Hei Ming it looked like the same expected output... My bad
6th May 2024, 3:19 PM
Mahmuda Akter Moon