# String Methods

> Python strings come with a rich set of built-in methods. These methods do not modify the original string (since strings are immutable) but return a new...

Canonical: https://www.internhack.in/learn/python/strings-and-string-methods/string-methods
Last updated: 2026-11-28

Difficulty: Beginner

Python strings come with a rich set of built-in methods. These methods do not modify the original string (since strings are immutable) but return a new string with the requested transformation applied.

Case-changing methods include upper() which converts all characters to uppercase, lower() which converts to lowercase, title() which capitalizes the first letter of each word, and capitalize() which capitalizes only the first character of the string. These are essential for normalizing user input - for example, comparing emails in a case-insensitive way.

Whitespace handling is done with strip(), lstrip(), and rstrip(). The strip() method removes leading and trailing whitespace (or specified characters) from a string. This is particularly important when processing user input or reading from files where extra spaces or newlines may appear.

Searching and modifying methods include find() which returns the index of the first occurrence of a substring (or -1 if not found), count() which returns the number of non-overlapping occurrences, replace() which substitutes all occurrences of one substring with another, split() which breaks a string into a list of substrings based on a delimiter, and join() which combines a list of strings into one string with a specified separator.

## Code examples

### Case Conversion Methods

```
text = "hello, World!"

print(text.upper())
print(text.lower())
print(text.title())
print(text.capitalize())
```

upper() converts all characters to uppercase, lower() to lowercase, title() capitalizes each word, and capitalize() only capitalizes the first character of the whole string.

### strip, split, and join

```
raw_input = "  apple, banana, cherry  "
cleaned = raw_input.strip()
fruits = cleaned.split(", ")
result = " | ".join(fruits)

print(cleaned)
print(fruits)
print(result)
```

strip() removes leading/trailing whitespace. split(', ') breaks on the comma-space delimiter. join() connects list items with ' | '.

### find, count, and replace

```
text = "the cat sat on the mat"

print(text.find("cat"))
print(text.find("dog"))
print(text.count("the"))
print(text.replace("cat", "dog"))
```

find() returns 4 (the index where 'cat' starts) or -1 when the substring is not found. count() finds 2 occurrences of 'the'. replace() substitutes 'cat' with 'dog'.

### Chaining String Methods

```
data = "  John DOE  "
result = data.strip().lower().replace(" ", "_")

print(result)
```

Methods can be chained because each one returns a new string. Here we strip whitespace, convert to lowercase, and replace spaces with underscores.

## Key points

- All string methods return new strings; they never modify the original string in place.
- split() with no arguments splits on any whitespace and removes empty strings from the result.
- Use in operator for simple existence checks (e.g., 'cat' in text) instead of find() when you don't need the index.
- replace() replaces all occurrences by default. Pass a third argument to limit replacements: text.replace('a', 'b', 1) replaces only the first 'a'.

## Concepts covered

upper, lower, strip, split, join, replace, find, count

Previous: [String Indexing & Slicing](https://www.internhack.in/learn/python/strings-and-string-methods/string-indexing-slicing) · Next: [String Formatting](https://www.internhack.in/learn/python/strings-and-string-methods/string-formatting) · [All of Strings & String Methods](https://www.internhack.in/learn/python/strings-and-string-methods)

## More Strings & String Methods lessons

- [String Operations](https://www.internhack.in/learn/python/strings-and-string-methods/string-operations)
- [String Basics](https://www.internhack.in/learn/python/strings-and-string-methods/string-basics)

## Related

- [Learn](https://www.internhack.in/learn)
- [Python](https://www.internhack.in/learn/python)
- [Strings & String Methods](https://www.internhack.in/learn/python/strings-and-string-methods)
