Learning Python – Post 4: Useful String Methods

Learning Python – Post 4: Useful String Methods

I’m not trying to learn every Python string method — just the ones that are actually useful when working with text.
Here are a few I’ve been practicing, with examples that helped me remember them.

upper() — Make It Loud

Converts all letters in a string to uppercase.

text = "hello world"
print(text.upper()) # HELLO WORLD

strip() — Clean the Edges

Removes any extra spaces at the start and end of a string.

messy = "   Python is fun   "
cleaned = messy.strip()
print(cleaned) # "Python is fun"

replace() — Swap Things Out

Replaces one part of the string with another. Great for removing characters too.

text = "L e a r n i n g"
no_spaces = text.replace(" ", "")
print(no_spaces) # "Learning"

find() — Where Is It?

Tells you the index of the first time something appears. Returns -1 if not found.

line = "I love Python"
print(line.find("Python")) # 7
print(line.find("Java")) # -1

split() — Break It Down

Breaks a string into a list of words, by spaces (or another separator).

sentence = "Keep it simple"
words = sentence.split()
print(words) # ['Keep', 'it', 'simple']

join() — Put It Back Together

Joins a list of words into a single string, using the separator you choose.

words = ["Keep", "it", "simple"]
line = " ".join(words)
print(line) # "Keep it simple"

startswith() — Does It Begin With…?

Returns True if the string starts with a certain value.

phrase = "Python is great"
print(phrase.startswith("Python")) # True

len() — Count Everything

Returns the total number of characters in a string (spaces included).

msg = "Hello!"
print(len(msg)) # 6

lower() – Make It Quiet

Converts all letters to lowercase.

pythonKopierenBearbeitenname = "BENgi"
print(name.lower())  # bengi

in – Is It There?

Checks whether something exists inside a string. Returns True or False.

text = "Python is awesome"
print("awesome" in text) # True
print("Java" in text) # False

isalpha() – Only Letters?

Returns True if all characters are letters (no spaces, no numbers).

word = "hello"
print(word.isalpha()) # True

word = "hello123"
print(word.isalpha()) # False

isdigit() – Only Numbers?

Checks if all characters are digits.

num = "2025"
print(num.isdigit()) # True

num = "20a5"
print(num.isdigit()) # False

islower() / isupper() – All Lower / All Upper?

Checks if the entire string is lowercase or uppercase.

msg = "hello"
print(msg.islower()) # True

shout = "HEY"
print(shout.isupper()) # True

title() – Like a Book Title

Capitalizes the first letter of each word.

text = "python for beginners"
print(text.title()) # Python For Beginners

capitalize() – First Word Only

Only the first character becomes uppercase; the rest lowercase.

text = "hELLO world"
print(text.capitalize()) # Hello world

Leave a Reply

Your email address will not be published. Required fields are marked *