Founding Batch Offer ยท HLD ยท LLD ยท System Design at โ‚น799 until 30 Nov

Remove First Character

Remove the first character of text with slicing. Students guard empty input, retain the remaining sequence, and see why a one-character result becomes empty.

BeginnerPhase 5 - Category 4: String TransformationExample 4 of 10
remove-first-character.py
1# Take string input
2s = input("Enter a string: ")
3
4# Remove first character
5if len(s) > 0:
6 result = s[1:]
7 print(f"After removal: {result}")
8else:
9 print("String is empty")

Output

Enter a string: Hello
After removal: ello

In this one

  • Exclude the first position
  • Use an open-ended slice
  • Handle one-character text

How it works

  1. 1

    Check s is nonempty.

  2. 2

    Create s[1:] as result.

  3. 3

    Print it or the empty-input message.

Edge cases

What if s has one character?

What if s has one character?

The result is empty.

What if s is already empty?

What if s is already empty?

The code prints String is empty.

What's going on

s[1:] starts at index one and continues to the end. It therefore omits the original index-zero character.

The source string is unchanged; result holds the shortened text. For cat, result is at. A one-character input leaves an empty result, which is valid. The explicit empty branch gives feedback rather than displaying an apparently blank transformation.

What if s has one character? The result is empty.

What if s is already empty? The code prints String is empty.

Quiz

Tap an option. The answer stays hidden until you pick.

Question 1

Which slice removes the first character?

Pick an answer to check it.

Question 2

What if s has one character?

Pick an answer to check it.

Questions people ask

What if s has one character?

The result is empty.

What if s is already empty?

The code prints String is empty.

Which slice removes the first character?

s[1:].