Skip to content
Programs · Program 8-3

Q.Program 8-3: Write a program to input a string from the user and print it in the reverse order without creating a new string.

Puducherry TnboardTextbookSubjective· 3mImportance★★★★★est
31% · 4/13 Questions
🔒 Locked · start free trial →

You're viewing a preview — the full solution, concept, methods & PYQ mapping are locked.

Start your 14-day free trial to unlock the full solution →

Use negative indexing to PRINT the existing string backwards — iterate i over range(-1, -len(st)-1, -1) and print st[i] with end='' — so no reversed copy is ever created.

Idea. The constraint "without creating a new string" rules out slicing tricks like st[::-1] (that builds a reversed copy). But printing needs no new string at all: Python's negative indices already address the string from the back — st[-1] is the last character, st[-2] the second last, … st[-len(st)] the first. Walking i from -1 down to -len(st) prints the original string's characters in reverse order.

st = input("Enter a string: ")
print("The string in reverse order is:")
for i in range(-1, -len(st) - 1, -1):
    print(st[i], end='')
print()   # final newline

Key lines:

  • range(-1, -len(st) - 1, -1) — start at -1, step -1, stop before -len(st)-1, i.e. the last value produced is -len(st). For a 5-character string: -1, -2, -3, -4, -5.
  • print(st[i], end='') — end='' suppresses print's default newline so the characters appear joined on one line.

Trace for st = "Hello" (len = 5):

ist[i]printed so far
-1oo
-2lol
-3loll
-4eolle
-5HolleH

Unlock everything free for 14 days

  • Full step-by-step solutions
  • Concept-first explanations
  • Methods, shortcuts & mistakes
  • PYQ mapping + timed mock tests

Full access for 14 days. No credit card required.