Python String Methods

Python String Methods

Strings are immutable sequences of characters that are used to store and manipulate text. With Python’s rich set of string methods, it is easier than ever to manipulate and extract information from strings. In this article, we will go through some of the most commonly used Python string methods.

Common String Methods

.lower() and .upper()

The lower() method returns a new string with all the characters converted to lowercase letters. Similarly, the upper() method returns a new string with all the characters converted to uppercase letters. Here’s an example:


string = "HELLO, WORLD!"
lowercase = string.lower()
uppercase = string.upper()

print(lowercase)   # "hello, world!"
print(uppercase)   # "HELLO, WORLD!"

.capitalize()

The capitalize() method returns a new string with the first character converted to uppercase and the rest of the characters converted to lowercase. Here’s an example:


string = "hello, world!"
capitalized = string.capitalize()

print(capitalized)   # "Hello, world!"

.title()

The title() method returns a new string where the first letter of each word is capitalized. Here’s an example:


string = "hello, world!"
title = string.title()

print(title)   # "Hello, World!"

.isalpha() and .isdigit()

The isalpha() method returns True if all the characters in a string are alphabets, and False otherwise. Similarly, the isdigit() method returns True if all the characters in a string are digits, and False otherwise. Here’s an example:


string1 = "hello"
string2 = "12345"

print(string1.isalpha())   # True
print(string2.isalpha())   # False

print(string1.isdigit())   # False
print(string2.isdigit())   # True

.split()

The split() method splits a string into a list of substrings based on a specified separator. The separator can be any whitespace character by default, or any other character specified inside the parentheses. Here’s an example:


string = "hello, world!"
split_string = string.split(",")

print(split_string)   # ["hello", " world!"]

.join()

The join() method is used to concatenate a list of strings into a single string using a specified separator. Here’s an example:


list_of_strings = ["hello", "world", "!"]
separator = " "

joined_string = separator.join(list_of_strings)

print(joined_string)   # "hello world !"

.find() and .replace()

The find() method returns the index of the first occurrence of a specified substring inside a string. If the substring is not found, it returns -1. The replace() method replaces all occurrences of a specified substring with another string. Here’s an example:


string = "hello, world!"
substring = "o"
replace_with = "x"

index = string.find(substring)
replaced_string = string.replace(substring, replace_with)

print(index)            # 4
print(replaced_string)  # "hellx, wxrld!"

Conclusion

These are just a few of the many Python string methods that are available. By using these string methods, you can easily manipulate and extract information from text strings in your Python programs.

Leave a Reply

Your email address will not be published. Required fields are marked *

Scroll to Top