🐍 String only manipulation

string formatting

Searching

greeting = "Hello"
 
pos = greeting.index("l")   # first 'l' -> 2
found = greeting.find("lo")  # returns 3 (or -1 if not found)

Replacing

new_word = greeting.replace("e", "a")   # 'Hallo'

Changing Case

"hello".upper()   # 'HELLO'
"WORLD".lower()   # 'world'
"hi there".title() # 'Hi There'
"test".capitalize() # 'Test'

Checking Content

"abc123".isalnum()  # True - only numbers or letters
"123".isdigit()     # True - only numbers
"hello".isalpha()   # True - pnly letters
"hello".startswith("he") # True
"hello".endswith("lo")   # True

Splitting & Joining

text = "Jim,Tom,Marcel"
names = text.split(",")        # ['Jim','Tom','Marcel']
joined = "-".join(names)        # 'Jim-Tom-Marcel'

Stripping

Remove unwanted characters

"  hello  ".strip()   # 'hello'
"xxhelloxx".strip("x") # 'hello'

f-Strings

⭐️ Make it easier to insert dynamic values into strings.

Basic Usage

food = "Bread"
person = "Jimmy"
 
f"Yesterday, {person} ate a {food}"
# Directly insert variables into the string

Expressions Inside f-Strings

f"Next year, {5 + 1} things will happen"
# Use math or any expression
 
f"{person.upper()} likes {food.lower()}"
# Call functions inside

Reusing Values

f"Yesterday, {person} ate a {food} and {person} loved it!"
# No need for index numbers or repeating .format arguments

f-Strings as Variables

text = f"Yesterday, {person} ate a {food}"
# Store an f-string in a variable after substitution

Using Dictionaries With f-Strings

data = {"name": "Jimmy", "food": "Bread"}
f"Yesterday, {data['name']} ate a {data['food']}"

Using f-Strings Inside Loops

for i in range(3):
    print(f"Item {i} processed!")

Alignment & Width

logs

⭐️ These tools help build readable output, tables, and nicely aligned console formatting.

f"Hello {person:10}!!"   # Right padding (width 10)
f"Hello {person:<10}!!"  # Left-aligned
f"Hello {person:>10}!!"  # Right-aligned
f"Hello {person:^10}!!"  # Centered

Formatting Numbers

⭐️ fStrings are also a great way to format numbers


Looping Through a String

Find here how to loop through a dictionary: Looping Through a String