+ 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='')
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)]
+ 2
many thanks to all for the replies
+ 1
Павел Никифоров just put ":" after.
https://code.sololearn.com/c5m98c4Ox95y/?ref=app
+ 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
+ 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
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?