Skip to content
Home Tutorials Roadmaps Courses
Log in Join free
Tutorials Python Strings
Python Beginner FREE

Strings

Lesson 1 of 14 Beginner Interactive

Strings are sequences of characters enclosed in quotes. They are immutable. Python supports single, double, and triple quotes.

Indexing & Slicing

Access characters by index: s[0] is the first character, s[-1] is the last. Slice with s[1:4].

Common Methods

  • .upper(), .lower() � change case
  • .strip() � remove whitespace
  • .split(), .replace(), .find()

F-Strings

Use f"Hello, {name}!" to embed expressions.

Syntax

PYTHON
s = "Hello, World!"
print(s[0])       # H
print(s[-1])      # !
print(s[0:5])     # Hello
print(s.upper())
print(s.split(", "))

name = "Alice"
print(f"Hello, {name}!")
Python Strings
PYTHON
text = "Hello, SukhNexus!"

print("Length:", len(text))
print("Upper:", text.upper())
print("Lower:", text.lower())
print("Title:", text.title())

# Slicing
print("Slice:", text[0:5])
print("From 7:", text[7:])
print("Reversed:", text[::-1])

# Methods
print("Count:", text.count("S"))
print("Find:", text.find("Sukh"))
print("Replace:", text.replace("Hello", "Hi"))
print("Split:", text.split(","))

# f-strings
name = "Alice"
score = 95.5
print(f"{name} scored {score:.1f}%")

Practice

1
Exercise

Reverse a string using slicing.

Answer
text = "Hello"
print(text[::-1])  # olleH
2
Exercise

Format a product line with f-string.

Answer
print(f"Item: Widget, Qty: 5, Total: ${5 * 9.99:.2f}")

Quick Quiz

1

What does "Python"[-1] return?

Negative indexing returns the last character, "n".

Interview Questions

Immutability enables dictionary keys, thread safety, and memory optimization through string interning.