String Methods in Python 🎯

beginner
8 min

String Methods in Python 🎯

Welcome to our in-depth guide on String Methods in Python! In this lesson, we'll explore various methods that allow us to manipulate, modify, and analyze strings in Python. By the end of this tutorial, you'll be comfortable using these methods in your own projects! 💡

What are Strings? 📝

Before we dive into string methods, let's quickly review what strings are in Python. Strings are sequences of characters, such as letters, digits, and symbols. You can create a string by enclosing characters in single quotes (') or double quotes (").

Basic String Operations 📝

Before we get to the methods, let's cover some basic operations we can perform on strings:

  • Concatenation (Joining Strings)

    python
    my_string1 = "Hello" my_string2 = "World" result = my_string1 + " " + my_string2 print(result) # Output: Hello World
  • Length of a String

    python
    my_string = "Python" length = len(my_string) print(length) # Output: 6
  • Accessing Characters in a String

    python
    my_string = "Python" first_char = my_string[0] last_char = my_string[-1] print(first_char, last_char) # Output: P y

String Methods 💡

Now, let's explore various string methods available in Python!

Capitalize()

The capitalize() method returns a copy of the string with the first character capitalized.

python
my_string = "python" result = my_string.capitalize() print(result) # Output: Python

Title()

The title() method returns a copy of the string with the first character of each word capitalized, the rest lowercase.

python
my_string = "hello world" result = my_string.title() print(result) # Output: Hello World

Upper() and Lower()

The upper() method returns a copy of the string in uppercase, while the lower() method returns a copy in lowercase.

python
my_string = "Python" result_upper = my_string.upper() result_lower = my_string.lower() print(result_upper, result_lower) # Output: PYTHON python

Swap Case

The swapcase() method returns a copy of the string with the uppercase characters converted to lowercase and lowercase characters to uppercase.

python
my_string = "Python" result = my_string.swapcase() print(result) # Output: pYTHON

Find()

The find() method returns the index of the first occurrence of the specified substring. If the substring is not found, it returns -1.

python
my_string = "Hello, Python World!" index = my_string.find("Python") print(index) # Output: 9

Replace()

The replace() method returns a copy of the string with all occurrences of the specified substring replaced by another substring.

python
my_string = "Hello, Python World!" new_string = my_string.replace("Python", "JavaScript") print(new_string) # Output: Hello, JavaScript World!

Count()

The count() method returns the number of occurrences of the specified substring in the string.

python
my_string = "Hello World Hello World" count = my_string.count("World") print(count) # Output: 2

Split()

The split() method splits the string into a list of substrings using a specified delimiter (default is a space).

python
my_string = "Hello, Python World!" result = my_string.split(", ") print(result) # Output: ['Hello', 'Python', 'World!']

Quiz 💡

Quick Quiz
Question 1 of 1

Which method returns a copy of the string with all occurrences of the specified substring replaced by another substring?

Summary ✅

In this comprehensive guide, we've covered various string methods in Python that help us manipulate, modify, and analyze strings. You've learned about basic operations and methods like capitalize(), title(), upper(), lower(), swapcase(), find(), replace(), count(), and split().

As you continue learning Python, remember to practice these methods and explore more advanced topics! Happy coding! 🎉