Learn Python
>>> data_structures.string_methods_slicing

String Methods & Slicing in Python

#concept

Strings support a rich set of built-in methods for searching, transforming, and inspecting text, plus a powerful slicing syntax for extracting any portion of a string by position. Slicing uses the same square-bracket syntax as single-character indexing, just with a start:stop:step range instead of one number.

#how it works
1

Basic indexing

s[0] accesses one character — the first one, at index 0.

2

Slicing extracts a range

s[start:stop] returns characters from start up to, but not including, stop.

3

Omitted bounds use defaults

Leaving out start or stop defaults to the beginning or end of the string.

4

A step value skips characters

s[start:stop:step] — a negative step even reverses direction.

5

Methods cover common text operations

.strip(), .split(), .replace(), and .find() handle most everyday needs.

#example
text = " Hello, World! "
print(text.strip())
print(text.strip().split(","))
print(text.strip().replace("World", "Python"))
 
word = "Python"
print(word[1:4])
print(word[::-1])
Output
Hello, World!
['Hello', ' World!']
Hello, Python!
yth
nohtyP
1.strip() removes leading and trailing whitespace, returning a new string.
2.split(",") breaks the stripped string apart at every comma, returning a list of pieces.
3.replace("World", "Python") returns a new string with every occurrence of "World" swapped for "Python".
4word[1:4] slices from index 1 up to (not including) index 4, giving "yth".
5word[::-1] uses a step of -1 with no start or stop, which reverses the entire string.
#variants

Searching Within a String

.find() returns the index of the first match (or -1 if not found), while in gives a quick yes/no answer.

sentence = "the quick brown fox"
print(sentence.find("brown"))
print("fox" in sentence)
Output
10
True

Case Conversion

.upper(), .lower(), and .title() return a new string with the case changed, leaving the original untouched.

name = "ada lovelace"
print(name.title())
Output
Ada Lovelace

Checking What a String Contains

Methods like .isdigit() and .isalpha() check whether every character fits a category — useful for quick input validation.

print("12345".isdigit())
print("hello".isalpha())
Output
True
True
#use cases
Cleaning up user input by stripping whitespace before validating it
Splitting a line of CSV or log data into its individual fields
Building a search feature that checks whether text contains a keyword
Reversing or extracting a substring, like a file extension, using slicing

Recommended resourcesAffiliate links · may earn us a commission

Recommended for learners

Structured coding coursesVideo courses that pair well with visual practice — pick one language and go deep.Browse courses Pro code editor & AI assistantWrite, debug and explain code faster while you practice the patterns here.Try it free Classic DSA interview booksThe reference books interviewers themselves learned from — great next to the visualizers.See the books

Practice what you learned

Re-implement each visualized algorithm in Python yourself — building beats watching.