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 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

MethodWhat 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 in to check text: "py" in "python".
  • Use split() and join() 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


๐Ÿ“š Related Post

โ† Back to python
Share: