Member-only story
Python String Methods Simplified with Related LeetCode Problems Solved


There are 45 string methods and it’s not possible to remember all of them but we will look at new ways to think about them that will make it easier.
Before we begin, note that a Python string is immutable. The original string is not changed when these methods are used.
String Methods can be classified into 4 categories for easy memory: RIMS
- String Returns Non-String — 7string methods that return a non-string namely: count() maketrans() partition() rpartition() split() splitlines() rsplit()
- String Is Something — 12 string methods that check if a string is something namely: isalnum() isalpha() isascii() isdecimal() isdigit() isidentifier() islower() isnumeric() isprintable() isspace() istitle() isupper()
- String Manipulation — 20 string methods to manipulate a string namely: capitalize() casefold() center() encode() expandtabs() format() format_map() join() ljust() lower() lstrip() replace() rjust() rstrip() strip() swapcase() title() translate() upper() zfill()
- String Searching — 6 string methods to search namely: endswith() find() index() rfind() rindex() startswith()
LeetCode Questions: try solving them on your own first!
- Final Value of Variable After Performing Operations
Link: https://leetcode.com/problems/final-value-of-variable-after-performing-operations/
Description: There is a programming language with only four operations and one variable X
:
++X
andX++
increments the value of the variableX
by1
.--X
andX--
decrements the value of the variableX
by1
.
Initially, the value of X
is 0
. Given an array of strings operations
containing a list of operations, return the final value of X
after performing all the operations.
String method(s) used: find()

2. Defanging an IP Address
Link: https://leetcode.com/problems/defanging-an-ip-address/submissions/