# Strings in Python — Indexing, Slicing and String Methods

Source: https://skills101.in/learn/python/python-strings — Skills101 free tutorial (Python Programming). Updated 2026-11-09.

A **string** is text: a sequence of characters inside quotes. Strings are **immutable** — you can't change a character in place; methods return a **new** string.

## 1. Indexing and slicing

![The word PYTHON with indexes 0 to 5 above and -6 to -1 below; slicing from 1 to 4 gives YTH, and a step of -1 reverses it](https://skills101.in/learn-img/python/string-index.svg)

```python
s = "PYTHON"
print(s[0], s[-1])     # first and last
print(s[1:4])          # index 1, 2, 3
print(s[:2], s[2:])    # start to 2, 2 to end
print(s[::2])          # every second character
print(s[::-1])         # reversed
print(len(s))
```

```text
P N
YTH
PY THON
PTO
NOHTYP
6
```

Slice rule: `s[start:stop:step]` — **start included, stop excluded**. Missing start = beginning; missing stop = end.

## 2. Useful string methods

```python
text = "  Learn Python at Skills101  "
clean = text.strip()
print(clean)
print(clean.lower())
print(clean.upper())
print(clean.replace("Python", "Java"))
print(clean.split())
print("-".join(["2026", "11", "08"]))
print(clean.startswith("Learn"), clean.endswith("101"))
print(clean.find("Python"), clean.count("n"))
```

```text
Learn Python at Skills101
learn python at skills101
LEARN PYTHON AT SKILLS101
Learn Java at Skills101
['Learn', 'Python', 'at', 'Skills101']
2026-11-08
True True
6 2
```

| Method | Does |
|---|---|
| `strip()` | remove spaces at both ends |
| `lower()`, `upper()`, `title()` | change case |
| `replace(a, b)` | replace all a with b |
| `split(sep)` | break into a list (default: spaces) |
| `sep.join(list)` | join a list into one string |
| `find(x)` | index of x, or -1 if missing |
| `count(x)` | how many times x appears |
| `isdigit()`, `isalpha()`, `isalnum()` | check the kind of characters |

## 3. Checking and looping

```python
pin = "302029"
print(pin.isdigit(), len(pin) == 6)

word = "programming"
vowels = 0
for ch in word:
    if ch in "aeiou":
        vowels += 1
print(vowels)
```

```text
True True
3
```

## 4. Strings are immutable

```python
name = "ravi"
# name[0] = "R"   ← TypeError: strings can't be changed in place
name = "R" + name[1:]   # build a new string instead
print(name)
```

```text
Ravi
```

## 5. Interview programs

### Palindrome check

```python
def is_palindrome(s):
    s = s.lower().replace(" ", "")
    return s == s[::-1]

print(is_palindrome("Madam"), is_palindrome("Never odd or even"), is_palindrome("Skills"))
```

```text
True True False
```

### Count each character

```python
from collections import Counter
print(Counter("banana").most_common())
```

```text
[('a', 3), ('n', 2), ('b', 1)]
```

### Reverse each word

```python
sentence = "learn to code"
print(" ".join(word[::-1] for word in sentence.split()))
```

```text
nrael ot edoc
```

## Common mistakes

- Expecting `s.upper()` to change `s` — it returns a new string; write `s = s.upper()`.
- Off-by-one in slices (stop is excluded).
- Using `+` in a loop to build big strings — collect parts in a list and `join` them.

## Quick recap

- Index from 0; negative indexes count from the end.
- `s[start:stop:step]`; `s[::-1]` reverses.
- Methods return new strings: strip, lower, upper, replace, split, join, find, count.
- Strings are immutable.


Practice quiz and free certificate: https://skills101.in/learn/python/python-strings