Skip to main content

Command Palette

Search for a command to run...

Day 3: Understanding Python Strings: Slicing, Indexing & Function Chaining

A beginner’s guide to working with strings in Python using slicing, indexing, and chained functions

Updated
1 min read
Day 3: Understanding Python Strings: Slicing, Indexing & Function Chaining

Today I learned about:

  1. Strings (Definition).

  2. String Slicing.

  3. Indexing in Strings.

  4. Chaining of Functions.

Definition:

In Python, anything in “ “ (double quotes) is a String data type. To make a multiline string, we can use “‘ “‘.

String Slicing & Indexing:

A string is stored in memory in the form of indexes. These are like placeholders in memory, as shown in the figure below.

The string shown in the figure above, when displayed partially, is called string slicing. We use the following syntax.

string[start : end : step]
  • start → index to begin (inclusive)

  • end → index to stop (exclusive)

  • step → how many characters to move each time (optional)

string [ :2]

The string above will mean printing the string from index value 0 to 2.

string [1: ]

The string above will mean printing the string from index value 1 to the last value of the string.

Chaining of Functions:

Chaining of functions means calling multiple string functions one after another on the same string in a single line of code. The output of one function becomes the input of the next function.

This helps in writing clean and efficient code and avoids the need for multiple lines.

string.strip().upper()