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 has many tools for working with text. These tools are called string methods.
In this post, I show you useful string operations with simple examples. ๐
๐ String Methods
Use a dot . after the string to call a method:
text = "hello"
print(text.upper()) # HELLO
String methods make a new string. They do not change the old string:
text = "hello"
text.upper()
print(text) # hello
text = text.upper()
print(text) # HELLO
๐ Change Letter Case
text = "python is fun"
print(text.upper()) # PYTHON IS FUN
print(text.lower()) # python is fun
print(text.capitalize()) # Python is fun
print(text.title()) # Python Is Fun
print("Hello".swapcase()) # hELLO
โ๏ธ Remove Space
strip() remove space from both ends. lstrip() remove from left. rstrip() remove from right.
name = " Dara "
print(name.strip()) # Dara
print(name.lstrip()) # Dara
print(name.rstrip()) # Dara
๐ 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)
๐ Find and Count Text
text = "hello python"
print(text.find("python")) # 6
print(text.find("java")) # -1 (not found)
print(text.index("hello")) # 0
print(text.count("l")) # 2
print(text.startswith("hello")) # True
print(text.endswith("on")) # True
find() give -1 when text not found. index() give an error.
โ๏ธ Split and Join
split() cut a string into a list. join() join strings from a list.
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']
๐ฅ Video: Python String Operations Part 2 (Khmer)
โ Check String
These methods return 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
Check user input before use:
age = input("Enter age: ")
if age.isdigit():
print("OK")
else:
print("Please enter numbers only")
โ๏ธ Fill and Align Text
print("5".zfill(3)) # 005
print("hi".center(10, "*")) # ****hi****
print("hi".ljust(5, ".")) # hi...
print("hi".rjust(5, ".")) # ...hi
๐ Get and Reverse Letters
String index start at 0. Negative index start from the end.
text = "python"
print(text[0]) # p
print(text[-1]) # n
print(text[0:3]) # pyt
print(text[::-1]) # nohtyp
print(len(text)) # 6
๐ฅ Video: Python String Operations Part 3 (Khmer)
๐ฅ Video: Generate Random Password with Random Module (Khmer)
๐ฅ Video: Using String Module Functions (Khmer)
๐ Quick Table
| Method | What it do |
|---|---|
upper() / lower() | Make letters big / small |
strip() | Remove space from both ends |
replace(a, b) | Replace a with b |
find(x) | Find x; give -1 if not found |
count(x) | Count x |
split(sep) | Cut string into a list |
join(list) | Join strings from a list |
startswith(x) / endswith(x) | Check start / end |
isdigit() / isalpha() | Check number / letter |
zfill(n) | Add zeros at the front |
โ Common Errors
Forget to save the result
name = "dara"
name.upper()
print(name) # dara
Save the new string:
name = name.upper()
print(name) # DARA
Change a string by index
Strings cannot change by index:
name = "dara"
name[0] = "D" # TypeError
Make a new string instead:
name = "D" + name[1:]
print(name) # Dara
Join numbers with join()
join() need strings. Change numbers to strings first:
nums = [1, 2, 3]
text = ",".join(str(n) for n in nums)
print(text) # 1,2,3
๐ก Tips
- Save the result of a string method in a variable.
- Clean user input with
strip(). - Use
lower()to compare text without case:answer.strip().lower() == "yes" - Use
into check text:"py" in "python". - Use
split()andjoin()to change separator:"a-b-c".split("-")then"+".join(...). - Check with
isdigit()before changing text to an integer. - Use
[::-1]to reverse a string. - You can use methods one after another:
" Hello ".strip().upper().
โ Any Question?
Tell us in comment below! ๐
#Python #String #StringMethod #LearnPython #Beginner