Placement Prep

Python String Slicing: Syntax, Examples, and Patterns

Learn Python string slicing: syntax, negative indices, step slices, and string reversal. Includes palindrome and word-reversal patterns for placement coding rounds.

By FACE Prep Team 4 min read
python string-slicing python-programs crt-training placement-prep

String slicing extracts any portion of a Python string in one expression; master the four parameters, and the palindrome check, the reversal, and the substring extraction all follow naturally.

Python’s campus placement coding tests at TCS, Infosys, Wipro, and Cognizant regularly include string manipulation problems. Slicing is the fastest tool in that toolkit, and understanding its exact semantics prevents the off-by-one errors that cost marks in timed rounds.

The s[start:stop] syntax

Python strings are zero-indexed sequences, meaning the first character sits at position 0. The slicing notation extends the familiar bracket syntax with a colon to define a range.

The basic form is:

s[start:stop]

start is the position of the first character to include. stop is the position where slicing stops, and that position is not included. A slice s[0:4] returns four characters at indices 0, 1, 2, and 3, not five.

Here is the index map for "FACE Prep" (length 9):

CharacterFACE Prep
Index012345678
s = "FACE Prep"

print(s[0:4])   # FACE
print(s[5:9])   # Prep
print(s[2:7])   # CE Pr

Output:

FACE
Prep
CE Pr

Both start and stop have defaults. Omit start and Python uses 0. Omit stop and Python uses len(s). Both omitted gives a copy of the full string.

s = "Python"

print(s[:4])    # Pyth
print(s[2:])    # thon

Output:

Pyth
thon

One edge case worth noting: if a slice index exceeds the string length, Python does not raise an IndexError. It silently clamps the index to the valid bound. The Python documentation on Common Sequence Operations covers this clamping behaviour as part of the general sequence protocol shared by strings, lists, and tuples.

Negative indices

Negative indices count backward from the end of the string. -1 is the last character, -2 is second-to-last, and so on.

CharacterPython
Index012345
Neg. index-6-5-4-3-2-1
s = "Python"

print(s[-1])      # n
print(s[-3])      # h
print(s[-4:-1])   # tho

Output:

n
h
tho

s[-4:-1] translates precisely: start at position 2 (four from the end), stop before position 5 (one from the end). Indices 2, 3, 4 give t, h, o.

You can mix positive and negative indices in the same slice. s[2:-2] on "Python" starts at index 2 and stops before index 4 (which is -2 from the end), returning "th".

s = "Python"

print(s[2:-2])    # th

Output:

th

Mixing is useful when you want to strip the first or last N characters without knowing the exact string length.

Step slicing and string reversal

The full syntax is s[start:stop:step]. The step parameter controls how many positions to advance for each character in the result. The default is 1, which steps through every character sequentially.

s = "Python"

print(s[::2])    # Pto
print(s[1::2])   # yhn

Output:

Pto
yhn

s[::2] starts at index 0 and takes every other character: indices 0, 2, 4 give P, t, o. s[1::2] starts at index 1: indices 1, 3, 5 give y, h, n.

A negative step value reverses the direction of traversal. Setting step to -1 moves right to left across the string.

s = "Python"

print(s[::-1])   # nohtyP

Output:

nohtyP

s[::-1] is the standard Python idiom for reversing a string. When step is negative, the default start becomes the last index and the default stop becomes before the first index, so the entire string is traversed in reverse.

One subtle distinction matters in practice: s[5:0:-1] on "Python" gives "nohty" because the stop index 0 is exclusive, cutting off the P. s[::-1] gives the full "nohtyP". Use s[::-1] when you want the complete reverse. The built-in slice() documentation covers the exact semantics of how defaults behave under negative step values.

Three patterns in placement rounds

String slicing reduces several common placement coding tasks to one-liners. These three appear in CSE and IT placement rounds at most major recruiters.

Palindrome check

A string is a palindrome if it reads the same forward and backward. The reversal idiom makes this a single comparison.

def is_palindrome(s):
    return s == s[::-1]

print(is_palindrome("racecar"))   # True
print(is_palindrome("python"))    # False
print(is_palindrome("madam"))     # True

Output:

True
False
True

"racecar" reversed is "racecar", so the comparison returns True. "python" reversed is "nohtyp", a different string, so it returns False. "madam" reversed is "madam", returning True.

Substring extraction

Slicing extracts a fixed-position segment without needing a loop or separate call to .find().

filename = "report_2026_final.txt"

year = filename[7:11]    # 2026
ext  = filename[-3:]     # txt

print(year)   # 2026
print(ext)    # txt

Output:

2026
txt

For "report_2026_final.txt" (length 21): index 7 is 2, index 10 is 6, so [7:11] captures the four digits. [-3:] starts at index 18 (t) and runs to the end, giving "txt".

Reverse word order

To reverse the order of words in a sentence, split on whitespace, reverse the resulting list with [::-1], then rejoin.

sentence = "Python is great for placements"
words = sentence.split()
reversed_sentence = " ".join(words[::-1])
print(reversed_sentence)   # placements for great is Python

Output:

placements for great is Python

sentence.split() gives ["Python", "is", "great", "for", "placements"]. Applying [::-1] to the list gives ["placements", "for", "great", "is", "Python"]. Joining with a space produces the reversed-word-order string.

The Python basic programs guide covers I/O handling, loop patterns, and string operations that appear before slicing problems in most placement coding rounds. The Python calculator program shows how conditional branching and input parsing fit alongside string work when building functional Python utilities.

The palindrome check above collapses to s == s[::-1] because the reversal idiom is that clean. If you want to go from knowing that to actually building LLM-powered text processing tools in Python, TinkerLLM is a Python-based LLM environment that starts at ₹299 and covers string manipulation, prompt construction, and text processing as applied to real AI tasks.

Primary sources

Frequently asked questions

What happens if a slice index is out of range?

Python silently clamps out-of-range indices to valid bounds rather than raising an IndexError. Asking for s[0:100] on a 6-character string returns the full string without an error.

Does string slicing modify the original string?

No. Python strings are immutable. Slicing always returns a new string object; the original string is unchanged. The reversal and extraction operations all create new string objects.

What is the difference between s[10:0:-1] and s[::-1]?

s[10:0:-1] stops before reaching index 0, so the first character of the string is excluded from the result. s[::-1] includes every character and produces the true full reverse.

How do I check if a string is a palindrome in Python?

Compare the string to its reverse: if the string equals its reverse, it is a palindrome. The expression s == s[::-1] returns True for racecar and False for python.

What does s[::2] return for a Python string?

s[::2] returns a new string containing characters at even-numbered positions: indices 0, 2, 4, and so on. For the string Python, s[::2] returns Pto.

Build AI projects

A self-paced playground for building with LLMs.

TinkerLLM is FACE Prep's sister property. A guided environment for shipping real LLM applications, the kind of project that earns a paragraph on your resume, not a line.

Try TinkerLLM (₹299 launch)
Free AI Roadmap PDF