In Python, strings are sequences of characters, like words or sentences. For example, "Hello, World!"
is a string.
Python provides several built-in methods to work with strings, such as changing their case, finding specific characters, or even removing parts of them. These methods are like tools that make working with text easier.
This guide will introduce you to various string methods in Python and explain how to use them with simple examples.
1. Creating Strings
Before we dive into string methods, let’s quickly see how to create strings.
# Example of creating a string
my_string = "Hello, World!"
print(my_string) # Output: Hello, World!
2. Basic String Methods
lower()
This method changes all letters in a string to lowercase.
my_string = "HELLO"
print(my_string.lower()) # Output: hello
upper()
This method changes all letters in a string to uppercase.
my_string = "hello"
print(my_string.upper()) # Output: HELLO
capitalize()
This method capitalizes the first letter of the string and makes all other letters lowercase.
my_string = "hello"
print(my_string.capitalize()) # Output: Hello
title()
This method capitalizes the first letter of each word in the string.
my_string = "hello world"
print(my_string.title()) # Output: Hello World
strip()
This method removes any leading or trailing whitespace (spaces, tabs, etc.) from the string.
my_string = " Hello, World! "
print(my_string.strip()) # Output: Hello, World!
replace()
This method replaces a specified part of the string with a new part.
my_string = "Hello, World!"
print(my_string.replace("World", "Python")) # Output: Hello, Python!
split()
This method splits the string into a list of words based on a specified separator (default is a space).
my_string = "Hello, World!"
print(my_string.split()) # Output: ['Hello,', 'World!']
3. Finding and Checking Characters
find()
This method searches for a substring and returns the index of the first occurrence. If not found, it returns -1
.
my_string = "Hello, World!"
print(my_string.find("World")) # Output: 7
print(my_string.find("Python")) # Output: -1
count()
This method counts how many times a substring appears in the string.
my_string = "Hello, World! Hello!"
print(my_string.count("Hello")) # Output: 2
startswith()
This method checks if the string starts with a given substring. It returns True
if it does and False
if it doesn’t.
my_string = "Hello, World!"
print(my_string.startswith("Hello")) # Output: True
print(my_string.startswith("world")) # Output: False
endswith()
This method checks if the string ends with a given substring.
my_string = "Hello, World!"
print(my_string.endswith("!")) # Output: True
isalpha()
This method checks if the string consists only of letters (a-z, A-Z).
my_string = "Hello"
print(my_string.isalpha()) # Output: True
my_string = "Hello123"
print(my_string.isalpha()) # Output: False
4. String Alignment and Padding
center()
This method centers the string, padding it with spaces or a specified character on both sides.
my_string = "Hello"
print(my_string.center(20, "*")) # Output: ****Hello*****
ljust()
This method left-aligns the string and pads it with a specified character.
my_string = "Hello"
print(my_string.ljust(10, "*")) # Output: Hello*****
rjust()
This method right-aligns the string and pads it with a specified character.
my_string = "Hello"
print(my_string.rjust(10, "*")) # Output: *****Hello
5. String Length and Formatting
len()
This is not a string method, but a built-in function in Python. It returns the length of a string (how many characters it contains).
my_string = "Hello"
print(len(my_string)) # Output: 5
format()
This method allows you to format strings by inserting variables into them.
name = "Clifford"
age = 25
greeting = "Hello, my name is {} and I am {} years old.".format(name, age)
print(greeting) # Output: Hello, my name is Clifford and I am 25 years old.
6. Checking for Digits and Numbers
isdigit()
This method checks if all characters in the string are digits (numbers).
my_string = "12345"
print(my_string.isdigit()) # Output: True
my_string = "123a5"
print(my_string.isdigit()) # Output: False
isnumeric()
This method checks if the string contains only numeric characters.
my_string = "12345"
print(my_string.isnumeric()) # Output: True
7. Joining Strings
join()
This method joins the elements of an iterable (like a list) into a string, separated by the specified character.
my_list = ["Hello", "World"]
separator = " "
print(separator.join(my_list)) # Output: Hello World