🐍 String & list manipulation

string list manipulation

Good to know

⭐️ All these logic is applicable to strings and lists (list, 2D list & tuples). However there are also some functions that only work for one of them. Find here more about specific logic:

Find here more specific possibilities for:

Basic Indexing

greeting = "Hello"
 
first = greeting[0]     # 'H'
last = greeting[-1]      # 'o'

Slicing

part = greeting[1:4]     # 'ell'
from_start = greeting[:3] # 'Hel'
to_end = greeting[2:]     # 'llo'

Length

length = len(greeting)   # 5

Reversing order

⭐️ This does not manipulate the original object but instead creates a new object. There is another way for reversing lists, but this approach then will directly manipulate the original list.

rev = greeting[::-1]   # 'olleH'