TGViewer
Python Learning Python Learning @python_bds · 5.74K subscribers
Post #1171 1.56K
🐍 Python Strings (Text Manipulation) 🔤

Strings are sequences of characters, used for handling text data. They are immutable, meaning once created, they cannot be changed directly. Python provides many built-in methods for working with strings.

👉 Essential for almost any program that interacts with text, from usernames to file paths.

🔹 1. What is a String?

A string is a sequence of characters, enclosed in single quotes '', double quotes "", or triple quotes """ """ for multi-line strings.

Example:
name = "Pythonista"
message = 'Hello, world!'
multiline = """This is
a multi-line
string."""
print(name)

Output: Pythonista

🔹 2. Accessing Characters (Indexing & Slicing)

• Indexing: Get a single character using its position (index starts at 0).
• Slicing: Get a sub-sequence of characters (a "slice") using [start:end]. The end index is exclusive.

Example:
word = "developer"
print(word[0]) # First character
print(word[3:7]) # Characters from index 3 up to (but not including) 7
print(word[-1]) # Last character

Output:
d
elop
r

🔹 3. String Concatenation & Length

• Concatenation: Joining strings using the + operator.
• len() function: Returns the number of characters in a string.

Example:
first = "Hello"
last = "World"
full_message = first + " " + last + "!"
print(full_message)
print(len(full_message))

Output:
Hello World!
12

🔹 4. Common String Methods

• .upper() / .lower(): Convert to uppercase/lowercase.
• .strip(): Removes leading/trailing whitespace.
• .replace(old, new): Replaces occurrences of a substring.
• .split(delimiter): Splits the string into a list of substrings.

Example:
text = "   Hello Python!   "
print(text.strip())
print("apple,banana,cherry".split(','))
print("Python".replace('o', '0'))

Output:
Hello Python!
['apple', 'banana', 'cherry']
Pyth0n

🔹 5. F-strings (Formatted String Literals)

A powerful and easy way to embed expressions inside string literals. Prefix the string with f or F.

Example:
item = "book"
price = 19.99
print(f"The {item} costs ${price:.2f}.") # .2f for 2 decimal places

Output: The book costs $19.99.

🎯 Today's Goal(What you should do)

✔️ Understand string immutability
✔️ Access characters and slice strings
✔️ Use common string methods for manipulation
✔️ Format strings with f-strings
  • ❤ 7
More from @python_bds
  1. Oct 8, 2026🐛 This Python Bug Looks Random, But It Isn't Look at this: functions = [] for i in range(…
  2. Oct 6, 2026🐍 Python Performance Optimization Python Performance Optimization: Make Your Code Faster…
  3. Oct 4, 2026Python Set Methods ✍️
  4. Oct 3, 2026Post #1310
  5. Oct 1, 2026🐍 Python Beginner Notes
  6. Sep 30, 2026🧠 return vs print() in Python These are not interchangeable. def add(a, b): print(a + b)…
Threads Profile ViewerView any public Threads profile without an account.Open ThreadLook →Writing with AI? Make it sound human.Metric37 rewrites AI drafts so they read naturally. Free AI detector, 1,500 words free.Try Metric37 →