Python string methods are built-in functions that allow you to manipulate strings in various ways. Strings in Python are immutable sequences of characters, and these methods provide a wide range of functionalities to perform operations such as string manipulation, searching, case conversion, and formatting.
Here’s the list of common Python string methods presented in a tabular format:
Method | Description |
---|---|
len() | Returns the length of the string. |
upper() | Converts all characters to uppercase. |
lower() | Converts all characters to lowercase. |
capitalize() | Converts the first character to uppercase and the rest to lowercase. |
title() | Converts the first character of each word to uppercase. |
strip() | Removes leading and trailing whitespace characters from the string. |
lstrip() | Removes leading whitespace characters from the string. |
rstrip() | Removes trailing whitespace characters from the string. |
split() | Splits the string into a list of substrings based on a separator. |
join() | Joins the elements of a list into a single string using a separator. |
replace() | Replaces occurrences of a substring with another substring. |
find() | Returns the index of the first occurrence of a substring. |
startswith() | Checks if the string starts with a specific substring. |
endswith() | Checks if the string ends with a specific substring. |
These string methods are used extensively in Python to perform common string operations and data processing tasks. They help in making the code concise and efficient when working with strings.
Python string methods example
Here are some examples of Python string methods:
len()
: Returns the length of the string.
string = "Hello, World!"
print(len(string)) # Output: 13
upper()
: Converts all characters to uppercase.
string = "hello, world!"
print(string.upper()) # Output: HELLO, WORLD!
lower()
: Converts all characters to lowercase.
string = "Hello, World!"
print(string.lower()) # Output: hello, world!
capitalize()
: Converts the first character to uppercase and the rest to lowercase.
string = "hello, world!"
print(string.capitalize()) # Output: Hello, world!
title()
: Converts the first character of each word to uppercase and the rest to lowercase.
string = "hello, world!"
print(string.title()) # Output: Hello, World!
strip()
: Removes leading and trailing whitespace characters from the string.
string = " Hello, World! "
print(string.strip()) # Output: "Hello, World!"
split()
: Splits the string into a list of substrings based on a specified separator (default is whitespace).
string = "apple,banana,orange"
print(string.split(",")) # Output: ['apple', 'banana', 'orange']
join()
: Joins the elements of a list into a single string using a specified separator.
list_of_words = ['Hello', 'World']
print(" ".join(list_of_words)) # Output: "Hello World"
replace()
: Replaces occurrences of a substring with another substring.
string = "Hello, World!"
print(string.replace("Hello", "Hi")) # Output: "Hi, World!"
find()
: Returns the index of the first occurrence of a substring (or -1 if not found).
string = "Hello, World!"
print(string.find("World")) # Output: 7
startswith()
: Checks if the string starts with a specific substring.
string = "Hello, World!"
print(string.startswith("Hello")) # Output: True
endswith()
: Checks if the string ends with a specific substring.
string = "Hello, World!"
print(string.endswith("World!")) # Output: True
These examples demonstrate the usage of various string methods in Python to manipulate strings, perform searching, and handle cases. You can try these examples in your Python environment to see the output.
# Example string
text = " Hello, World! "
# Length Method
print("Length of the string:", len(text))
# Case Conversion Methods
print("Uppercase:", text.upper())
print("Lowercase:", text.lower())
print("Capitalized:", text.capitalize())
print("Title Case:", text.title())
# Whitespace Manipulation Methods
print("Stripped:", text.strip())
print("Left Stripped:", text.lstrip())
print("Right Stripped:", text.rstrip())
# Splitting and Joining Methods
words = text.split(",")
print("Split Words:", words)
print("Joined Words:", "-".join(words))
# Substring Manipulation Methods
new_text = text.replace("Hello", "Hi")
print("Replaced Text:", new_text)
# Finding Substring
index = text.find("World")
if index != -1:
print("Substring 'World' found at index:", index)
else:
print("Substring 'World' not found")
# Check Startswith and Endswith
print("Starts with 'Hello':", text.startswith("Hello"))
print("Ends with 'World!':", text.endswith("World!"))
Output:
You can try running this code and see how each method works.
Note: IDE: PyCharm 2021.3.3 (Community Edition)
Windows 10
Python 3.10.1
All Python Examples are in Python 3, so Maybe its different from python 2 or upgraded versions.