Programming Examples

Write a Python program that:


Write a Python program that:

(a) Accepts a sentence from the user as input.

(b) Displays the first 5 characters and the last 5 characters of the sentence using slicing.

(c) Concatenates the sentence with another predefined string.

Solution

sentence = input("Enter a sentence: ")

# Display first 5 characters
first_five = sentence[:5]

# Display last 5 characters
last_five = sentence[-5:]

print("First 5 characters:", first_five)
print("Last 5 characters:", last_five)

# Concatenate with another predefined string
extra = " - Thank you!"
new_sentence = sentence + extra

print("Concatenated sentence:", new_sentence)
▶ RUN

Output/ Explanation:

Enter a sentence: Welcome to Python Programming

First 5 characters: Welco

Last 5 characters: mming

Concatenated sentence: Welcome to Python Programming - Thank you!

Latest Current Affairs 2026 Online Exam Quiz for One day Exam Online Typing Test CCC Online Test Python Programming Tutorials Best Computer Training Institute in Prayagraj (Allahabad) Online MBA 2 years Online MCA Online BCA Best Website and Software Company in Allahabad