Skip to main content

Command Palette

Search for a command to run...

Python Basics — Day 9 Strings in Python

Updated
2 min read
S

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

Python Basics

Part 9 of 21

A collection of study notes from my university and self-learning journey. This series covers Python fundamentals step by step — from setup to core concepts — to help both myself and others build a solid foundation in coding.

Up next

Python Basics — Day 10 Dictionaries in Python

01. What is a Dictionary? A dictionary is a data structure that stores data as Key:Value pairs. Defined using curly braces {}. student = { "name": "Sabin", "age": 35, "major": "Computer Science" } 👉 Key Points: Keys must be immutab...

More from this blog

S

Sabin’s DevLog

21 posts

I plan to upload everything I study and prepare here. I hope it can be helpful to others, and I also want my future self to see the learning journey I’ve been through.