DEV Community

qing
qing

Posted on

10 Python String Methods You Should Know

10 Python String Methods You Should Know

As a Python developer, working with strings is an essential part of your daily routine. Python provides a wide range of string methods that can help you manipulate and process strings efficiently. In this article, we'll explore 10 Python string methods that you should know.

1. upper() and lower()

These methods are used to convert a string to uppercase or lowercase, respectively.

my_string = "Hello World"
print(my_string.upper())  # Output: HELLO WORLD
print(my_string.lower())  # Output: hello world
Enter fullscreen mode Exit fullscreen mode

2. split() and join()

The split() method splits a string into a list of substrings based on a specified separator, while the join() method concatenates a list of strings into a single string.

my_string = "apple,banana,cherry"
fruits = my_string.split(",")
print(fruits)  # Output: ['apple', 'banana', 'cherry']

fruits = ["apple", "banana", "cherry"]
my_string = ",".join(fruits)
print(my_string)  # Output: apple,banana,cherry
Enter fullscreen mode Exit fullscreen mode

3. strip()

This method removes leading and trailing whitespace from a string.

my_string = "   Hello World   "
print(my_string.strip())  # Output: Hello World
Enter fullscreen mode Exit fullscreen mode

4. replace()

The replace() method replaces all occurrences of a substring with another substring.

my_string = "Hello World"
print(my_string.replace("World", "Universe"))  # Output: Hello Universe
Enter fullscreen mode Exit fullscreen mode

5. find()

This method returns the index of the first occurrence of a substring in a string. If the substring is not found, it returns -1.

my_string = "Hello World"
print(my_string.find("World"))  # Output: 6
print(my_string.find("Universe"))  # Output: -1
Enter fullscreen mode Exit fullscreen mode

6. index()

The index() method returns the index of the first occurrence of a substring in a string. If the substring is not found, it raises a ValueError.

my_string = "Hello World"
print(my_string.index("World"))  # Output: 6
# print(my_string.index("Universe"))  # Raises ValueError
Enter fullscreen mode Exit fullscreen mode

7. count()

This method returns the number of occurrences of a substring in a string.

my_string = "Hello World, Hello Universe"
print(my_string.count("Hello"))  # Output: 2
Enter fullscreen mode Exit fullscreen mode

8. startswith() and endswith()

These methods check if a string starts or ends with a specified substring.

my_string = "Hello World"
print(my_string.startswith("Hello"))  # Output: True
print(my_string.endswith("World"))  # Output: True
Enter fullscreen mode Exit fullscreen mode

9. isalpha() and isdigit()

The isalpha() method checks if a string contains only alphabets, while the isdigit() method checks if a string contains only digits.

my_string = "HelloWorld"
print(my_string.isalpha())  # Output: True

my_string = "12345"
print(my_string.isdigit())  # Output: True
Enter fullscreen mode Exit fullscreen mode

10. format()

This method formats a string by replacing placeholders with values.

my_string = "Hello, {}!"
print(my_string.format("World"))  # Output: Hello, World!
Enter fullscreen mode Exit fullscreen mode

In conclusion, these 10 Python string methods can help you perform various string operations, from simple conversions to complex formatting. Mastering these methods can make you a more efficient and effective Python developer.

Follow for more Python!


💡 Related: **Content Creator Ultimate Bundle (Save 33%)* — $29.99*

Top comments (0)