08 How to Use String Operations in Python + Tips (Video in Khmer)
Learn Python string operations: upper, lower, strip, replace, split, join, find and more. Simple example and tips. Good for beginner!
2026-10-21 ยท 5 min read
Hello friend! ๐ Python give you many tool to work with text. We call them string method.
In this post, I show you string operations in Python. Easy and short. ๐
๐ Before Start
String method use dot . after the string.
text = "hello"
print(text.upper()) # HELLO
Important: String cannot change. Method give back new string. The old one is same.
text = "hello"
text.upper()
print(text) # hello (not change!)
text = text.upper()
print(text) # HELLO (save to variable)
๐ Part 1: Case, Strip and Replace
text = "python is fun"
print(text.upper()) # PYTHON IS FUN
print(text.lower()) # python is fun
print(text.capitalize()) # Python is fun (first letter only)
print(text.title()) # Python Is Fun (every word)
print("Hello".swapcase()) # hELLO (swap big and small)
โ๏ธ Remove Space with strip()
name = " Dara "
print(name.strip()) # "Dara"
print(name.lstrip()) # "Dara " (left only)
print(name.rstrip()) # " Dara" (right only)
๐ Replace Text
text = "I like Java"
print(text.replace("Java", "Python")) # I like Python
print("a-b-c".replace("-", "")) # abc
๐ฅ Video: Python String Operations Part 1 (Khmer)
๐ Part 2: Find, Count, Split and Join
Find Text
text = "hello python"
print(text.find("python")) # 6 (index where it start)
print(text.find("java")) # -1 (not found)
print(text.index("hello")) # 0
print(text.count("l")) # 2 (how many time)
print(text.startswith("hello")) # True
print(text.endswith("on")) # False
find() give -1 if not found. index() give error.
Split and Join
split() cut string into list. join() put list back into string.
text = "apple,banana,orange"
fruits = text.split(",")
print(fruits) # ['apple', 'banana', 'orange']
print(" - ".join(fruits)) # apple - banana - orange
print("I love Python".split()) # ['I', 'love', 'Python'] (cut by space)
๐ฅ Video: Python String Operations Part 2 (Khmer)
โ Part 3: Check and Align
Check Type of String
Result is True or False.
print("123".isdigit()) # True
print("abc".isalpha()) # True
print("abc123".isalnum()) # True
print("hello".islower()) # True
print("HELLO".isupper()) # True
print(" ".isspace()) # True
Good for check user input:
age = input("Enter age: ")
if age.isdigit():
print("OK")
else:
print("Please enter number only")
Fill and Align
print("5".zfill(3)) # 005
print("hi".center(10, "*")) # ****hi****
print("hi".ljust(5, ".")) # hi...
print("hi".rjust(5, ".")) # ...hi
Slice and Reverse
text = "python"
print(text[0]) # p
print(text[-1]) # n
print(text[0:3]) # pyt
print(text[::-1]) # nohtyp (reverse)
print(len(text)) # 6
๐ฅ Video: Python String Operations Part 3 (Khmer)
๐ Quick Table
| Method | What it do |
|---|---|
upper() / lower() | Change to big / small letter |
strip() | Remove space at start and end |
replace(a, b) | Change a to b |
find(x) | Find index of x |
count(x) | Count x |
split(sep) | Cut string into list |
join(list) | Join list into string |
startswith(x) / endswith(x) | Check start / end |
isdigit() / isalpha() | Check number / letter |
zfill(n) | Fill 0 at front |
โ Common Error
Forget to save the result:
name = "dara"
name.upper()
print(name) # dara (not change)
Fix: name = name.upper()
Change string by index:
name = "dara"
name[0] = "D" # TypeError
Fix: name = "D" + name[1:] or name.capitalize()
Use join() with number:
nums = [1, 2, 3]
print(",".join(nums)) # TypeError
print(",".join(str(n) for n in nums)) # 1,2,3
๐ก Tips
- Method not change original string. Always save result in variable.
- Use
strip()to clean user input. Uselower()to compare text:if answer.strip().lower() == "yes": - Use
into check text fast:"py" in "python" - Use
split()+join()to change separator:"a-b-c".split("-")then"+".join(...) - Use
isdigit()before change text toint(). No crash! - Use
[::-1]to reverse string quick. - Can chain method:
" Hello ".strip().upper()โHELLO - Forget method name? Type
dir(str)orhelp(str.upper).
โ Any Question?
Tell us in comment below! ๐
#Python #String #StringMethod #LearnPython #Beginner