Python Basics
Python String Methods
Python strings come with many built-in methods that let you clean, search and transform text without writing loops yourself.
Changing Case
The upper() and lower() methods return a new string in all uppercase or all lowercase, which is handy for comparing text regardless of case.
text = "Learn Python"
print(text.upper()) # LEARN PYTHON
print(text.lower()) # learn pythonRemoving Extra Spaces
strip() removes whitespace from the start and end of a string, which is very useful when cleaning up user input.
raw = " hello world "
print(raw.strip()) # "hello world"Splitting and Joining
split() breaks a string into a list based on a separator, while join() does the opposite — it combines a list of strings into one string.
sentence = "python is fun"
words = sentence.split()
print(words) # ['python', 'is', 'fun']
joined = "-".join(words)
print(joined) # python-is-funReplacing and Finding Text
msg = "I love Java"
print(msg.replace("Java", "Python")) # I love Python
print(msg.find("love")) # 2Quick Reference Table
| Method | What it Does | Example |
|---|---|---|
| upper() | Converts to uppercase | "hi".upper() → "HI" |
| lower() | Converts to lowercase | "HI".lower() → "hi" |
| strip() | Removes surrounding whitespace | " hi ".strip() → "hi" |
| split() | Splits string into a list | "a,b".split(",") → ["a","b"] |
| join() | Joins a list into a string | "-".join(["a","b"]) → "a-b" |
| replace() | Replaces text | "hi".replace("h","H") → "Hi" |
| find() | Returns index of substring | "hi".find("i") → 1 |
| format() | Inserts values into a string | "{}".format("hi") → "hi" |
String methods never change the original string — they always return a brand new one, since strings are immutable in Python.
