</>
Skip to content
DSA lessons (8/55)

DSA — Strings

What is a String?

A sequence of characters.

# Creating strings
s = "Hello, World!"
s = str(123)

String Operations

OperationTime Complexity
AccessO(1)
ConcatenationO(n)
SearchO(n)
SliceO(k)

Common Operations

# Length
len(s)

# Access
first = s[0]
last = s[-1]

# Slice
substring = s[1:5]

# Find
index = s.find("World")
count = s.count("l")

# Replace
new_s = s.replace("World", "Python")

# Split
words = s.split(", ")

# Join
new_s = ", ".join(words)

# Case
s.upper()
s.lower()
s.title()

String Problems

# Check palindrome
def is_palindrome(s):
    return s == s[::-1]

# Count vowels
def count_vowels(s):
    return sum(1 for c in s.lower() if c in 'aeiou')

# Reverse string
def reverse_string(s):
    return s[::-1]

Mini Practice

  1. Manipulate strings
  2. Check for palindromes
  3. Count character frequencies
  4. Reverse a string

Up Next

Continue with Linked Lists — linked list data structure.

Related Topics

Frequently Asked Questions about Strings

What is Strings in DSA?

Strings is a fundamental concept in DSA. This lesson explains it step by step with clear examples, making it easy for beginners to understand.

How do I learn Strings?

Start by reading the explanation above, then try the code examples. Practice by modifying the examples and experimenting with different values. Hands-on practice is the best way to learn Strings.

Why is Strings important in DSA?

Strings is essential for DSA development. Understanding this concept will help you write better code and solve real-world problems more effectively.