Print First Word

Print first word of string.

BeginnerPhase 5 - Category 3: Word OperationsExample 1 of 10
print-first-word.py
1# Take string input
2s = input("Enter a string: ")
3
4# Get first word
5words = s.split()
6if len(words) > 0:
7 print(f"First word: {words[0]}")
8else:
9 print("No words found")

Output

Enter a string: Hello World Programming
First word: Hello

What's going on

Split string and get first element.

Key Concepts:

split() splits into words
words[0] is first word
Check if words exist