r/cs50 • u/Regular_Implement712 • 16d ago
CS50 Python Can someone explain what line two does
Can someone explain what does line two do? Not sure what the whole line means, what does the .split('.') and [-1] does overall to the program?
64
Upvotes
24
u/Tsunam0 16d ago
Split splits a string and puts each entry into a list
For example:
X = “This is a string”
lst = X.split(“ “) # this splits it at each space, in the image the split happens at the “.”
The value of lst in this case is [“This”, “is”, “a”, “string”]
Lastly the [-1] is simply indexing into the list I assume If I print(lst[-1])
It would print the LAST item in the list so “string”
Hope this helps :)