In Python (programming language), string methods are built-in functions used to perform operations on strings such as converting case, searching text, replacing characters, and formatting.
Strings are immutable, meaning the original string cannot be changed. Instead, string methods return a new string.
1οΈβ£ upper() β Convert to Uppercase
text = "hello python"
print(text.upper())
Output
HELLO PYTHON
2οΈβ£ lower() β Convert to Lowercase
text = "HELLO PYTHON"
print(text.lower())
Output
hello python
3οΈβ£ capitalize() β Capitalize First Letter
text = "python programming"
print(text.capitalize())
Output
Python programming
4οΈβ£ title() β Capitalize Each Word
text = "python programming language"
print(text.title())
Output
Python Programming Language
5οΈβ£ strip() β Remove Extra Spaces
text = " python "
print(text.strip())
Output
python
6οΈβ£ replace() β Replace Characters
text = "I like Java"
print(text.replace("Java", "Python"))
Output
I like Python
7οΈβ£ split() β Split String into List
text = "apple banana mango"
print(text.split())
Output
['apple', 'banana', 'mango']
8οΈβ£ join() β Join List into String
words = ["Python", "is", "fun"]
sentence = " ".join(words)
print(sentence)
Output
Python is fun
9οΈβ£ find() β Find Position of a Word
text = "Python programming"
print(text.find("programming"))
Output
7
π count() β Count Occurrences
text = "apple apple banana"
print(text.count("apple"))
Output
2
1οΈβ£1οΈβ£ startswith() β Check Beginning
text = "Python programming"
print(text.startswith("Python"))
Output
True
1οΈβ£2οΈβ£ endswith() β Check Ending
text = "report.pdf"
print(text.endswith(".pdf"))
Output
True
β Example Program
text = " python programming "
print(text.strip())
print(text.upper())
print(text.replace("python", "Java"))
Output
python programming
PYTHON PROGRAMMING
Java programming
β Common String Methods Summary
| Method | Description |
|---|---|
upper() | Convert to uppercase |
lower() | Convert to lowercase |
capitalize() | Capitalize first letter |
title() | Capitalize every word |
strip() | Remove spaces |
replace() | Replace text |
split() | Convert string to list |
join() | Join list into string |
find() | Find position |
count() | Count occurrences |