DSA: The 8-Week Grind — Day 5

SAI GOUTHAMSAI GOUTHAM
4 min read

Mastering Python String & List Methods — Your Daily DSA Toolkit

“The better you know your tools, the faster you solve the problem.”


🟡 Why This Day Is Important

You’ll be solving hundreds of problems using strings and lists in Python.
Knowing the right built-in methods can help you:

  • Avoid writing unnecessary loops

  • Write clean, readable code

  • Debug faster under pressure

Today, I’m covering the essential string and list methods every DSA coder needs.


🔹 STRING METHODS


len() — Get length of a string

Explanation:
Returns the number of characters in a string.

pythonCopyEdittext = "goutham"
print(len(text))  # Output: 7

lower() and upper() — Convert case

Explanation:
Converts all letters to lowercase or uppercase.

pythonCopyEditprint("Hello".lower())  # Output: hello
print("world".upper())  # Output: WORLD

strip(), lstrip(), rstrip() — Remove whitespace

Explanation:
Removes unwanted spaces from the beginning and/or end of a string.

pythonCopyEditmsg = "   spaced   "
print(msg.strip())   # Output: "spaced"
print(msg.lstrip())  # Output: "spaced   "
print(msg.rstrip())  # Output: "   spaced"

startswith() and endswith() — Check prefixes/suffixes

Explanation:
Returns True if the string starts or ends with a specified substring.

pythonCopyEditname = "goutham"
print(name.startswith("gou"))  # Output: True
print(name.endswith("ham"))    # Output: True

find() — Find the first index of a substring

Explanation:
Returns the starting index of the first occurrence, or -1 if not found.

pythonCopyEdittxt = "datastructures"
print(txt.find("struct"))  # Output: 4
print(txt.find("z"))       # Output: -1

replace() — Replace part of a string

Explanation:
Replaces a target substring with a new one.

pythonCopyEdits = "I love Java"
print(s.replace("Java", "Python"))  # Output: I love Python

split() — Break string into a list

Explanation:
Splits a string into a list of words based on a separator (default: space).

pythonCopyEditsentence = "hello world"
words = sentence.split()
print(words)  # Output: ['hello', 'world']

join() — Merge list into a string

Explanation:
Joins list items into one string, separated by the given separator.

pythonCopyEditjoined = "-".join(['hello', 'world'])
print(joined)  # Output: hello-world

isdigit(), isalpha(), isalnum() — Check content type

Explanation:
Check whether the string contains only digits, letters, or both.

pythonCopyEditprint("123".isdigit())     # Output: True
print("abc".isalpha())     # Output: True
print("abc123".isalnum())  # Output: True

🔹 LIST METHODS


append() — Add an item to the end

Explanation:
Adds a new element to the end of the list.

pythonCopyEditarr = [1, 2]
arr.append(3)
print(arr)  # Output: [1, 2, 3]

insert(index, value) — Add at a specific position

Explanation:
Inserts an element at the specified index.

pythonCopyEditarr = [1, 3]
arr.insert(1, 2)
print(arr)  # Output: [1, 2, 3]

pop() — Remove item by index (default: last)

Explanation:
Removes and returns the element at the given index.

pythonCopyEditarr = [1, 2, 3]
arr.pop()
print(arr)  # Output: [1, 2]

remove(value) — Remove by value

Explanation:
Removes the first occurrence of a value.

pythonCopyEditarr = [1, 2, 2, 3]
arr.remove(2)
print(arr)  # Output: [1, 2, 3]

sort() — Sort in-place

Explanation:
Sorts the list in ascending order.

pythonCopyEditnums = [3, 1, 2]
nums.sort()
print(nums)  # Output: [1, 2, 3]

reverse() — Reverse in-place

Explanation:
Reverses the elements of the list.

pythonCopyEditnums.reverse()
print(nums)  # Output: [3, 2, 1]

count() — Count frequency of an element

Explanation:
Returns how many times an element appears.

pythonCopyEditdata = [1, 2, 2, 3]
print(data.count(2))  # Output: 2

index() — Get index of first occurrence

Explanation:
Returns the index of the first occurrence of an element.

pythonCopyEditprint(data.index(3))  # Output: 3

extend() — Merge two lists

Explanation:
Adds elements of one list to the end of another.

pythonCopyEdita = [1, 2]
b = [3, 4]
a.extend(b)
print(a)  # Output: [1, 2, 3, 4]

copy() — Shallow copy

Explanation:
Creates a new list with the same elements.

pythonCopyEditlst = [1, 2, 3]
copied = lst.copy()
print(copied)  # Output: [1, 2, 3]

clear() — Remove all elements

Explanation:
Deletes all items in the list.

pythonCopyEditlst.clear()
print(lst)  # Output: []

🟢 Day 5 Takeaways

✅ I now know all the essential string and list methods that show up in DSA problems
✅ These tools will make my future code cleaner, faster, and easier to debug
✅ I’m ready to apply them in actual problems starting tomorrow


0
Subscribe to my newsletter

Read articles from SAI GOUTHAM directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

SAI GOUTHAM
SAI GOUTHAM

💻 Experienced Computer Science graduate with 3+ years in software engineering, specializing in full-stack web development and cloud solutions. 🥇 Proficient in Python, JavaScript, and SQL, with expertise in React.js, Node.js, Django, and Flask. 🎖️ Skilled in optimizing system performance and deploying scalable applications using AWS. Strong background in agile methodologies and DevOps practices. 🥅 Committed to delivering high-quality, efficient, and scalable software solutions.