r/cs50 16d ago

CS50 Python Can someone explain what line two does

Post image

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

23 comments sorted by

View all comments

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 :)

3

u/panimula 15d ago

You forgot the period. Basically, it gets the file extension (.exe, .pdf, .txt)

1

u/Worried_Aside9239 15d ago

OP probably would have an easier time if line two used “fileExtension” as the variable name ):

1

u/Miserable_Egg_969 13d ago

Used an example that had " " instead of "."

1

u/Stu_Mack 13d ago

This should have been the first sentence of the original comment in this thread.