Here's a simple Python code that takes a sentence as input from the user and then prints the length of the first word in that sentence:
# Prompt the user to enter a sentence
sentence = input("Enter a sentence: ")
# Split the sentence using space and get the first word
first_word = sentence.split(" ")[0]
# Calculate the length of the first word
length_of_first_word = len(first_word)
# Print the length of the first word
print(length_of_first_word)
Explanation:
- The code prompts the user to enter a sentence.
- The
split(" ")
method is used to break the sentence into words at each space. This returns a list of words. - The first word is accessed with
sentence.split(" ")[0]
. - The
len()
function is used to calculate the length of the first word. - Finally, the length is printed.
Sample Runs:
- If the user inputs
Everything in its right place
, the output will be9
. - If the user inputs
Down is the new up
, the output will be4
.