dev.rean.me
python

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

Share:

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

MethodWhat 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. Use lower() to compare text: if answer.strip().lower() == "yes":
  • Use in to check text fast: "py" in "python"
  • Use split() + join() to change separator: "a-b-c".split("-") then "+".join(...)
  • Use isdigit() before change text to int(). No crash!
  • Use [::-1] to reverse string quick.
  • Can chain method: " Hello ".strip().upper() โ†’ HELLO
  • Forget method name? Type dir(str) or help(str.upper).

โ“ Any Question?

Tell us in comment below! ๐Ÿ‘‡

#Python #String #StringMethod #LearnPython #Beginner


๐Ÿ“š Related Post

โ† Back to python
Share: