[Solved] Python How to get the last word from a sentence in python? [closed]


You have to pass “/” as a separator parameter to the split function: split("/").
The split function by default splits by whitespace ” “.

"Hello World".split() => ["Hello", "World"]
"Hello/World".split() => ["Hello/World"]
"Hello/World".split("/") => ["Hello", "World"]

Change your code to:

print(sentence.split("/")[-1])

3

solved Python How to get the last word from a sentence in python? [closed]