String Methods & Slicing in Python
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.
Basic indexing
s[0] accesses one character — the first one, at index 0.
Slicing extracts a range
s[start:stop] returns characters from start up to, but not including, stop.
Omitted bounds use defaults
Leaving out start or stop defaults to the beginning or end of the string.
A step value skips characters
s[start:stop:step] — a negative step even reverses direction.
Methods cover common text operations
.strip(), .split(), .replace(), and .find() handle most everyday needs.
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])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)Case Conversion
.upper(), .lower(), and .title() return a new string with the case changed, leaving the original untouched.
name = "ada lovelace"print(name.title())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())Recommended resourcesAffiliate links · may earn us a commission
Recommended for learners
Practice what you learned
Re-implement each visualized algorithm in Python yourself — building beats watching.