# Day 13 – String Methods in Python

![](https://cdn.hashnode.com/uploads/covers/664f77938fc1f806b829b90b/4032f0d5-cb36-4349-b5bb-3f67c5b884eb.jpg align="center")

Welcome to **Day 13 of 100 Days of Python!**

In the previous lessons, we learned how to create strings, access individual characters, find their length, and extract parts of strings using slicing.

Today, we will learn about **string methods**.

Python provides many built-in methods that make it easier to work with and process strings.

These methods can help us:

*   Change the case of text
    
*   Remove characters from strings
    
*   Replace text
    
*   Split strings into multiple parts
    
*   Search for text
    
*   Count occurrences
    
*   Check the contents of a string
    
*   Check how a string starts or ends
    
*   Format text
    
*   And much more
    

One important thing to remember is that **strings are immutable in Python**.

This means that string methods do not modify the original string. Instead, methods such as `upper()`, `lower()`, and `replace()` return a **new string**.

* * *

## 1\. What Are String Methods?

A string method is a function that is associated with a string and can be called using the dot `.` operator.

For example:

```python
name = "sritesh"

print(name.upper())
```

Output:

```text
SRITESH
```

The general syntax is:

```python
string.method()
```

Some methods also accept arguments:

```python
string.method(argument)
```

For example:

```python
name.replace("sritesh", "ron")
```

String methods are extremely useful when processing text in Python.

* * *

## 2\. Strings Are Immutable

Strings in Python are **immutable**.

Immutable means that once a string has been created, its contents cannot be directly changed.

For example:

```python
name = "Sritesh"
```

We cannot modify one character of the existing string directly.

Instead, string methods create a new string.

For example:

```python
name = "sritesh"

new_name = name.upper()

print(name)
print(new_name)
```

Output:

```text
sritesh
SRITESH
```

The original `name` remains unchanged.

This is an important concept when working with Python strings.

* * *

## 3\. `upper()`

The `upper()` method converts all applicable characters in a string to uppercase.

Example:

```python
str1 = "AbcDEfghIJ"

print(str1.upper())
```

Output:

```text
ABCDEFGHIJ
```

In our program:

```python
a = "!!!Sritesh!! !!!!!!!!! Sritesh!!!"

print(a.upper())
```

Output:

```text
!!!SRITESH!! !!!!!!!!! SRITESH!!!
```

The method converts the letters to uppercase while leaving punctuation and spaces unchanged.

* * *

## 4\. `lower()`

The `lower()` method converts all applicable characters in a string to lowercase.

Example:

```python
str1 = "AbcDEfghIJ"

print(str1.lower())
```

Output:

```text
abcdefghij
```

For example:

```python
a = "!!!Sritesh!! !!!!!!!!! Sritesh!!!"

print(a.lower())
```

Output:

```text
!!!sritesh!! !!!!!!!!! sritesh!!!
```

* * *

## 5\. `rstrip()`

The `rstrip()` method removes specified trailing characters from the **right side** of a string.

For example:

```python
str3 = "Hello !!!"

print(str3.rstrip("!"))
```

Output:

```text
Hello 
```

In our program:

```python
a = "!!!Sritesh!! !!!!!!!!! Sritesh!!!"

print(a.rstrip("!"))
```

The exclamation marks at the end of the string are removed.

Characters elsewhere in the string are not removed.

The name `rstrip` can be understood as:

```text
r → right
strip → remove
```

So `rstrip()` removes characters from the right side.

* * *

## 6\. `replace()`

The `replace()` method replaces occurrences of one string with another string.

The basic syntax is:

```python
string.replace(old, new)
```

For example:

```python
str2 = "Silver Spoon"

print(str2.replace("Sp", "M"))
```

Output:

```text
Silver Moon
```

In our program:

```python
a = "!!!Sritesh!! !!!!!!!!! Sritesh!!!"

print(a.replace("Sritesh", "Ron"))
```

Output:

```text
!!!Ron!! !!!!!!!!! Ron!!!
```

Every occurrence of `"Sritesh"` is replaced with `"Ron"`.

Remember that `replace()` returns a new string rather than modifying the original string.

* * *

## 7\. `split()`

The `split()` method divides a string into multiple parts and returns the result as a **list**.

For example:

```python
str2 = "Silver Spoon"

print(str2.split(" "))
```

Output:

```text
['Silver', 'Spoon']
```

Here, `" "` is used as the separator.

So:

```text
Silver Spoon
```

is split into:

```text
Silver
Spoon
```

and returned as a list:

```python
['Silver', 'Spoon']
```

In our program:

```python
a = "!!!Sritesh!! !!!!!!!!! Sritesh!!!"

print(a.split(" "))
```

The string is split wherever a space occurs.

This method is very useful when converting a sentence into individual words.

* * *

## 8\. `capitalize()`

The `capitalize()` method converts the first character of a string to uppercase and converts the remaining characters to lowercase.

For example:

```python
str1 = "hello"

print(str1.capitalize())
```

Output:

```text
Hello
```

Consider another example:

```python
str2 = "hello WorlD"

print(str2.capitalize())
```

Output:

```text
Hello world
```

Notice that the `W`, `D`, and other uppercase letters after the first character are converted to lowercase.

In our program:

```python
blogHeading = "introduction tO Python"

print(blogHeading.capitalize())
```

Output:

```text
Introduction to python
```

This demonstrates that `capitalize()` affects the entire string, not just the first character.

* * *

## 9\. `center()`

The `center()` method centers a string inside a field of a specified width.

For example:

```python
str1 = "Welcome to the Console!!!"

print(str1.center(50))
```

The string is positioned in the center of a field containing 50 characters.

We can also specify a padding character.

```python
str1 = "Welcome to the Console!!!"

print(str1.center(50, "."))
```

Output:

```text
............Welcome to the Console!!!.............
```

The `.` characters fill the remaining space around the string.

We can also check the length:

```python
print(len(str1))
print(len(str1.center(50)))
```

The original string has a smaller length, while the centered result has a total width of `50`.

* * *

## 10\. `count()`

The `count()` method returns the number of times a particular value occurs in a string.

For example:

```python
str2 = "Abracadabra"

countStr = str2.count("a")

print(countStr)
```

Output:

```text
4
```

There are four lowercase `"a"` characters in `"Abracadabra"`.

In our program:

```python
a = "!!!Sritesh!! !!!!!!!!! Sritesh!!!"

print(a.count("Sritesh"))
```

The result tells us how many times `"Sritesh"` occurs in the string.

* * *

## 11\. `endswith()`

The `endswith()` method checks whether a string ends with a particular value.

It returns:

```text
True
```

if the condition is satisfied, otherwise:

```text
False
```

For example:

```python
str1 = "Welcome to the Console !!!"

print(str1.endswith("!!!"))
```

Output:

```text
True
```

We can also specify a start and end position:

```python
str1 = "Welcome to the Console !!!"

print(str1.endswith("to", 4, 10))
```

Output:

```text
True
```

This checks the specified portion of the string.

* * *

## 12\. `find()`

The `find()` method searches for the **first occurrence** of a value in a string.

If the value is found, it returns its index.

If the value is not found, it returns:

```text
-1
```

For example:

```python
str1 = "He's name is Dan. He is an honest man."

print(str1.find("is"))
```

Output:

```text
10
```

The first occurrence of `"is"` starts at index `10`.

If we search for something that does not exist:

```python
print(str1.find("ishh"))
```

Output:

```text
-1
```

This is an important difference between `find()` and `index()`.

* * *

## 13\. `find()` vs `index()`

Both `find()` and `index()` can be used to locate a substring.

However, they behave differently when the substring is not found.

### Using `find()`

```python
str1 = "Hello Python"

print(str1.find("Java"))
```

Output:

```text
-1
```

`find()` returns `-1` when the value is absent.

### Using `index()`

```python
str1 = "Hello Python"

print(str1.index("Java"))
```

This raises a `ValueError` because `"Java"` does not exist in the string.

So remember:

| Method | If value is found | If value is not found |
| --- | --- | --- |
| `find()` | Returns index | Returns `-1` |
| `index()` | Returns index | Raises `ValueError` |

* * *

## 14\. `isalnum()`

The `isalnum()` method checks whether **all characters** in a string are alphanumeric.

Alphanumeric characters include:

*   `A-Z`
    
*   `a-z`
    
*   `0-9`
    

For example:

```python
str1 = "WelcomeToTheConsole"

print(str1.isalnum())
```

Output:

```text
True
```

There are only letters in the string, so the result is `True`.

Another example:

```python
str1 = "Welcome00"

print(str1.isalnum())
```

This also returns:

```text
True
```

because both letters and numbers are alphanumeric.

If the string contains spaces or punctuation, `isalnum()` returns `False`.

* * *

## 15\. `isalpha()`

The `isalpha()` method checks whether **all characters** in a string are alphabetic.

For example:

```python
str1 = "Welcome"

print(str1.isalpha())
```

Output:

```text
True
```

But:

```python
str1 = "Welcome00"

print(str1.isalpha())
```

returns:

```text
False
```

because the string contains numbers.

A simple way to remember the difference is:

```text
isalnum() → letters + numbers
isalpha() → letters only
```

* * *

## 16\. `islower()`

The `islower()` method checks whether all cased characters in a string are lowercase.

For example:

```python
str1 = "hello world"

print(str1.islower())
```

Output:

```text
True
```

If the string contains uppercase letters, the result will be `False`.

For example:

```python
print("Hello world".islower())
```

Output:

```text
False
```

* * *

## 17\. `isprintable()`

The `isprintable()` method checks whether all characters in a string are printable.

For example:

```python
str1 = "We wish you a Merry Christmas"

print(str1.isprintable())
```

Output:

```text
True
```

However, control characters such as a newline can make the result `False`.

In our program:

```python
str1 = "We wish you a Merry Christmas\n"

print(str1)
print(str1.isprintable())
```

The string contains `\n`, which represents a newline character.

Therefore, `isprintable()` returns:

```text
False
```

This is a useful example of how invisible control characters can affect string checks.

* * *

## 18\. `isspace()`

The `isspace()` method returns `True` if all characters in the string are whitespace characters.

Whitespace can include:

*   Spaces
    
*   Tabs
    
*   Newline characters
    

For example:

```python
str1 = "        "

print(str1.isspace())
```

Output:

```text
True
```

A string containing only whitespace is therefore considered a whitespace string.

* * *

## 19\. `istitle()`

The `istitle()` method checks whether a string is written in **title case**.

For example:

```python
str1 = "World Health Organization"

print(str1.istitle())
```

Output:

```text
True
```

Each word begins with an uppercase letter.

Now consider:

```python
str2 = "To kill a Mocking bird"

print(str2.istitle())
```

Output:

```text
False
```

The capitalization does not follow title-case formatting.

* * *

## 20\. `isupper()`

The `isupper()` method checks whether all cased characters in a string are uppercase.

For example:

```python
str1 = "WORLD HEALTH ORGANIZATION"

print(str1.isupper())
```

Output:

```text
True
```

If the string contains lowercase letters, the result is `False`.

For example:

```python
print("Hello World".isupper())
```

Output:

```text
False
```

* * *

## 21\. `startswith()`

The `startswith()` method checks whether a string starts with a particular value.

It returns `True` or `False`.

For example:

```python
str1 = "Python is a Interpreted Language"

print(str1.startswith("Python"))
```

Output:

```text
True
```

Because the string begins with `"Python"`.

If we check:

```python
print(str1.startswith("Java"))
```

the result is:

```text
False
```

This method is useful when checking prefixes in text.

* * *

## 22\. `swapcase()`

The `swapcase()` method changes uppercase characters to lowercase and lowercase characters to uppercase.

For example:

```python
str1 = "Python is a Interpreted Language"

print(str1.swapcase())
```

Output:

```text
pYTHON IS A iNTERPRETED lANGUAGE
```

So:

```text
Uppercase → lowercase
Lowercase → uppercase
```

* * *

## 23\. `title()`

The `title()` method converts a string into title case.

It makes the first letter of each word uppercase.

For example:

```python
str1 = "He's name is Dan. Dan is an honest man."

print(str1.title())
```

Output:

```text
He'S Name Is Dan. Dan Is An Honest Man.
```

Notice that `title()` treats the text after the apostrophe as part of the title-case transformation, which can produce results such as:

```text
He'S
```

This is an example where a string method may behave differently from how we might manually format natural language.

* * *

## 24\. Complete Program

Here is the complete program from today's practice:

```python
# Strings are immutable in Python, meaning that once a string is created,
# it cannot be changed. However, you can create new strings based on
# existing ones using various string methods.

a = "!!!Sritesh!! !!!!!!!!! Sritesh!!!"

print(len(a))

print(a.upper())
print(a.lower())

print(a.rstrip("!"))

print(a.replace("Sritesh", "Ron"))

print(a.split(" "))

blogHeading = "introduction tO Python"
print(blogHeading.capitalize())

str1 = "Welcome to the Console !!!"
print(str1.center(50))
print(len(str1))
print(len(str1.center(50)))

print(a.count("Sritesh"))

print(str1.endswith("!!!"))

str1 = "Welcome to the Console !!!"
print(str1.endswith("to", 4, 10))

str1 = "He's name is Dan. He is an honest man."
print(str1.find("is"))
print(str1.find("ishh"))

str1 = "WelcomeToTheConsole"
print(str1.isalnum())

str1 = "Welcome00"
print(str1.isalpha())

str1 = "hello world"
print(str1.islower())

str1 = "We wish you a Merry Christmas\n"
print(str1)
print(str1.isprintable())

str1 = "        "
print(str1.isspace())

str2 = "        "
print(str2.isspace())

str1 = "World Health Organization"
print(str1.istitle())

str2 = "To kill a Mocking bird"
print(str2.istitle())

str1 = "WORLD HEALTH ORGANIZATION"
print(str1.isupper())

str1 = "Python is a Interpreted Language"
print(str1.startswith("Python"))

str1 = "Python is a Interpreted Language"
print(str1.swapcase())

str1 = "He's name is Dan. Dan is an honest man."
print(str1.title())
```

* * *

## 25\. Important String Methods

Here is a quick reference for the methods covered today:

| Method | Purpose |
| --- | --- |
| `upper()` | Converts text to uppercase |
| `lower()` | Converts text to lowercase |
| `rstrip()` | Removes specified trailing characters |
| `replace()` | Replaces occurrences of text |
| `split()` | Splits a string into a list |
| `capitalize()` | Capitalizes the first character and lowercases the rest |
| `center()` | Centers a string within a specified width |
| `count()` | Counts occurrences of a value |
| `endswith()` | Checks whether a string ends with a value |
| `find()` | Finds the first occurrence; returns `-1` if absent |
| `index()` | Finds the first occurrence; raises `ValueError` if absent |
| `isalnum()` | Checks for only alphanumeric characters |
| `isalpha()` | Checks for only alphabetic characters |
| `islower()` | Checks whether the string is lowercase |
| `isprintable()` | Checks whether all characters are printable |
| `isspace()` | Checks whether all characters are whitespace |
| `istitle()` | Checks whether the string is title case |
| `isupper()` | Checks whether the string is uppercase |
| `startswith()` | Checks whether a string starts with a value |
| `swapcase()` | Swaps uppercase and lowercase |
| `title()` | Converts text to title case |

* * *

## 26\. Useful Groups of String Methods

It can be easier to remember these methods by grouping them.

### Changing Case

```python
upper()
lower()
capitalize()
swapcase()
title()
```

### Searching and Counting

```python
find()
index()
count()
```

### Checking a String

```python
isalnum()
isalpha()
islower()
isprintable()
isspace()
istitle()
isupper()
```

### Checking Position

```python
startswith()
endswith()
```

### Modifying or Processing Text

```python
replace()
split()
rstrip()
center()
```

* * *

## 27\. Quick Revision

### Convert to uppercase

```python
text.upper()
```

### Convert to lowercase

```python
text.lower()
```

### Remove trailing characters

```python
text.rstrip("!")
```

### Replace text

```python
text.replace("old", "new")
```

### Split text

```python
text.split(" ")
```

### Capitalize the first character

```python
text.capitalize()
```

### Center text

```python
text.center(50)
```

### Count occurrences

```python
text.count("Python")
```

### Check the ending

```python
text.endswith("!")
```

### Find text

```python
text.find("Python")
```

### Check alphanumeric characters

```python
text.isalnum()
```

### Check alphabetic characters

```python
text.isalpha()
```

### Check lowercase

```python
text.islower()
```

### Check uppercase

```python
text.isupper()
```

### Check the beginning

```python
text.startswith("Python")
```

### Change character case

```python
text.swapcase()
```

### Convert to title case

```python
text.title()
```

* * *

## 28\. Key Takeaways

After completing Day 13, you should understand:

*   What string methods are
    
*   How to call a method using the `.` operator
    
*   That strings are immutable in Python
    
*   How `upper()` and `lower()` change letter casing
    
*   How `rstrip()` removes trailing characters
    
*   How `replace()` replaces text
    
*   How `split()` converts a string into a list
    
*   How `capitalize()` changes the capitalization of a string
    
*   How `center()` positions text within a specified width
    
*   How `count()` counts occurrences
    
*   How `startswith()` and `endswith()` check string boundaries
    
*   How `find()` searches for text
    
*   The difference between `find()` and `index()`
    
*   How `isalnum()` and `isalpha()` validate string contents
    
*   How `islower()` and `isupper()` check character case
    
*   How `isprintable()` checks printable characters
    
*   How `isspace()` checks whitespace
    
*   How `istitle()` checks title case
    
*   How `swapcase()` reverses character casing
    
*   How `title()` converts text to title case
    

String methods are essential for text processing in Python. These methods will become especially useful when working with **user input, files, data cleaning, validation, and automation**.

As we continue through the 100 Days of Python challenge, these methods will become building blocks for larger programs and real-world applications.

* * *

## 📂 Day 13 Resources

All notes and code for this day are available in the GitHub repository:

%[https://github.com/SriteshSuranjan/100-Days-of-Python/tree/main/13-Day13-String-Methods] 

* * *
