Python provides many built-in methods that allow you to perform operations on strings.
String methods return new values and do not change the original string.
Convert to Uppercase
The upper() method converts a string to uppercase.
Example:
text = "hello world"
print(text.upper())
Output:
HELLO WORLD
Convert to Lowercase
The lower() method converts a string to lowercase.
Example:
text = "HELLO WORLD"
print(text.lower())
Output:
hello world
Remove Whitespace
The strip() method removes whitespace from the beginning and end of a string.
Example:
text = " Hello World "
print(text.strip())
Output:
Hello World
Replace a String
The replace() method replaces one string with another.
Example:
text = "Hello World"
print(text.replace("World", "Python"))
Output:
Hello Python
Split a String
The split() method splits a string into a list.
Example:
text = "Apple,Banana,Mango"
print(text.split(","))
Output:
['Apple', 'Banana', 'Mango']
Capitalize First Letter
The capitalize() method converts the first character to uppercase.
Example:
text = "python"
print(text.capitalize())
Output:
Python
Title Case
The title() method converts the first character of each word to uppercase.
Example:
text = "welcome to python"
print(text.title())
Output:
Welcome To Python
Swap Case
The swapcase() method converts uppercase letters to lowercase and vice versa.
Example:
text = "Hello World"
print(text.swapcase())
Output:
hELLO wORLD
Count Occurrences
The count() method returns the number of times a value appears in a string.
Example:
text = "apple apple banana"
print(text.count("apple"))
Output:
2
Find a String
The find() method searches for a value and returns its position.
Example:
text = "Hello World"
print(text.find("World"))
Output:
6
If the value is not found, it returns -1.
Check Start of String
The startswith() method returns True if the string starts with the specified value.
Example:
text = "Hello World"
print(text.startswith("Hello"))
Output:
True
Check End of String
The endswith() method returns True if the string ends with the specified value.
Example:
text = "Hello World"
print(text.endswith("World"))
Output:
True
Check if String is Alphabetic
The isalpha() method returns True if all characters are letters.
Example:
text = "Python"
print(text.isalpha())
Output:
True
Check if String is Numeric
The isdigit() method returns True if all characters are numbers.
Example:
text = "12345"
print(text.isdigit())
Output:
True
Join Strings
The join() method joins elements of an iterable into a string.
Example:
items = ["Python", "Java", "PHP"]
print(", ".join(items))
Output:
Python, Java, PHP
Common String Methods
| Method | Description |
|---|---|
upper() | Converts string to uppercase |
lower() | Converts string to lowercase |
strip() | Removes whitespace |
replace() | Replaces text |
split() | Splits string into a list |
capitalize() | Capitalizes first character |
title() | Capitalizes each word |
swapcase() | Swaps letter cases |
count() | Counts occurrences |
find() | Finds position of text |
startswith() | Checks starting text |
endswith() | Checks ending text |
isalpha() | Checks if alphabetic |
isdigit() | Checks if numeric |
join() | Joins iterable elements |
Summary
- String methods help manipulate and analyze text.
- Methods do not modify the original string.
- Common methods include
upper(),lower(),strip(),replace(), andsplit(). - Methods like
find(),count(), andstartswith()help search strings. - Validation methods such as
isalpha()andisdigit()check string content.