Print Last Word

Print last word of string.

Logic BuildingBeginner
Logic Building
# Take string input
s = input("Enter a string: ")

# Get last word
words = s.split()
if len(words) > 0:
    print(f"Last word: {words[-1]}")
else:
    print("No words found")

Output

Enter a string: Hello World Programming
Last word: Programming

Split string and get last element.

Key Concepts:

  • split() splits into words
  • words[-1] is last word
  • Negative indexing