Python in Plain English

New Python content every day. Follow to join our 3.5M+ monthly readers.

Follow publication

Python String Methods Simplified with Related LeetCode Problems Solved

Srikanth Potukuchi
Python in Plain English
5 min readMay 10, 2022

--

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

  1. String Returns Non-String — 7string methods that return a non-string namely: count() maketrans() partition() rpartition() split() splitlines() rsplit()
  2. 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()
  3. 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()
  4. String Searching — 6 string methods to search namely: endswith() find() index() rfind() rindex() startswith()

LeetCode Questions: try solving them on your own first!

  1. 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 and X++ increments the value of the variable X by 1.
  • --X and X-- decrements the value of the variable X by 1.

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/

--

--

Published in Python in Plain English

New Python content every day. Follow to join our 3.5M+ monthly readers.

Written by Srikanth Potukuchi

I am working as a Lead Data Scientist in Canada. Passionate about data science since 2014. I started with SQL , R and SAS; later picked up Python.

No responses yet

Write a response