+ 1

explain how the third line of code works. specifically [:int

words = input() if len(words)%4==0: print(words[:int(len(words)/-2)]) else: print(words[-2:],sep='')

6th May 2022, 9:11 PM
Павел Никифоров
Павел Никифоров - avatar
6 ответов
+ 2
Павел Никифоров Line 3 prints a slice of the original input if that input is divisible by 4. So a string slice normally looks like this -> word[:2] The example I have given will print the first 2 letters of the input. Your line 3 wants to print half of the letters of the input, hence len(word)/2 But this will create a float which cannot be used in a slice, so the float must be converted to an integer Thus [: int(float)]
6th May 2022, 10:01 PM
Rik Wittkopp
Rik Wittkopp - avatar
+ 2
many thanks to all for the replies
7th May 2022, 7:50 AM
Павел Никифоров
Павел Никифоров - avatar
7th May 2022, 7:34 AM
Ashkan Shakibi
Ashkan Shakibi - avatar
+ 1
As Ashkan Sh 🇺🇦🇺🇦🇺🇦 has already shown, replace line 3 with print(words[int(len(words)/-2):]) Review list slicing in your Python tutorials. String slicing is the same
7th May 2022, 7:49 AM
Rik Wittkopp
Rik Wittkopp - avatar
+ 1
Павел Никифоров Lesson 34.1 in Python for Beginners has the informatiin you need to review. The lesson is called List Slicing, but the principles also apply to Strings
7th May 2022, 7:53 AM
Rik Wittkopp
Rik Wittkopp - avatar
0
thank you very much for your response. and here's something else. is it possible to display the second half of the word instead of the first and how to implement it?
7th May 2022, 1:42 AM
Павел Никифоров
Павел Никифоров - avatar