Python Basics — Day 9 Strings in Python
Aspiring Full-Stack Developer | Python · Django · React · SQL | documenting my learning journey
Building skills in Python and full-stack development, with a focus on web apps and system design
Junior developer in training — Python, Django, React — preparing for a career in full-stack engineering
From Python basics to full-stack projects, sharing my progress as I grow into a developer
Future full-stack engineer | Learning in public: Python, APIs, Databases, and Web Development
01. What is a String?
A string is a sequence of characters (text data).
Defined using either single quotes
' 'or double quotes" ".
text1 = "Hello"
text2 = 'Python'
print(text1, text2)
02. Indexing & Slicing Strings
- Strings are ordered data types, so you can access characters by index (just like lists).
word = "Python"
print(word[0]) # P (first character)
print(word[-1]) # n (last character)
print(word[0:4]) # Pyth (index 0 to 3)
print(word[:2]) # Py
print(word[2:]) # thon
03. String Operations
a = "Hello"
b = "World"
print(a + " " + b) # Concatenation → Hello World
print(a * 3) # Repetition → HelloHelloHello
04. String Methods
Python provides many built-in methods for strings.
s = " python programming "
print(s.upper()) # Convert to uppercase
print(s.lower()) # Convert to lowercase
print(s.strip()) # Remove leading/trailing spaces
print(s.replace("python", "java")) # Replace substring
print(s.split()) # Split by spaces → ['python', 'programming']
05. String Checks
msg = "Hello Python"
print("Python" in msg) # True
print("Java" not in msg) # True
print(msg.startswith("Hello")) # True (check start)
print(msg.endswith("Python")) # True (check end)
06. Using f-strings
As introduced on Day 4, f-strings are very handy with strings.
name = "Sabin"
lang = "Python"
print(f"{name} is studying {lang}.")
07. Loops with Strings
Strings are iterable, so you can loop through each character.
for ch in "Hello":
print(ch)
08. Practice Examples
Example 1: String Length
s = "Python"
print(len(s)) # 6
Example 2: Extract Username from Email
email = "sabin@test.com"
user = email.split("@")[0]
print("Username:", user) # sabin
Example 3: Reverse a String
text = "Python"
print(text[::-1]) # nohtyP