dev.rean.me
python

06 How to Use Operators with String and List in Python + Tips (Video in Khmer)

Learn Python operators with string and list. Use +, *, in, and comparison with simple example and tips. Good for beginner!

2026-10-20 ยท 4 min read

Share:

Hello friend! ๐Ÿ‘‹ + and * work with number. They also work with string and list.

In this post, I show you operators with string and list in Python. Easy and short. ๐Ÿ


๐ŸŽฅ Video: Using Operators with String and List in Python (Khmer)

๐ŸŽฅ Video: Arithmetic with Math and Boolean in Python (Khmer)

๐ŸŽฅ Video: Arithmetic Operators in Python (Khmer)


โž• Operator + (Join)

Join two string or two list.

print("Hello" + " " + "World")    # Hello World
print([1, 2] + [3, 4])            # [1, 2, 3, 4]

Join only same type. Number must change to string first:

print("Age: " + str(20))          # Age: 20
print("Age: " + 20)               # TypeError
print([1, 2] + 3)                 # TypeError

โœ–๏ธ Operator * (Repeat)

Repeat string or list many time.

print("Ha" * 3)        # HaHaHa
print("-" * 5)         # -----
print([0] * 3)         # [0, 0, 0]
print([1, 2] * 2)      # [1, 2, 1, 2]

๐Ÿ” Operator in and not in (Check)

Check if item exist. Result is True or False.

# string: check text inside text
print("py" in "python")           # True

# list: check item inside list
fruits = ["apple", "banana"]
print("apple" in fruits)          # True
print("mango" not in fruits)      # True

Use with if:

if "apple" in fruits:
    print("We have apple")

โš–๏ธ Comparison Operator

Use ==, !=, <, > to compare.

print("abc" == "abc")            # True
print("abc" != "ABC")            # True (big and small letter different)
print([1, 2] == [2, 1])          # False (order matter)

String compare by alphabet:

print("apple" < "banana")        # True
print("a" < "B")                 # False (big letter has smaller code)

๐Ÿ”— Operator += and *=

Short way to update value.

text = "Hi"
text += " there"
print(text)                      # Hi there

nums = [1, 2]
nums += [3, 4]
print(nums)                      # [1, 2, 3, 4]

line = "="
line *= 5
print(line)                      # =====

๐Ÿ“ Useful Function

print(len("python"))             # 6
print(min([5, 2, 8]))            # 2
print(max("abc"))                # c

โŒ Common Error

String cannot change by index:

name = "Dara"
name[0] = "S"                    # TypeError

Error:

TypeError: 'str' object does not support item assignment

Fix: make new string:

name = "S" + name[1:]
print(name)                      # Sara

But list can change:

nums = [1, 2, 3]
nums[0] = 99
print(nums)                      # [99, 2, 3]

๐Ÿ’ก Tips

  • String cannot change. List can change.
  • + join same type only: string + string, list + list.
  • Use str() to change number to string before join.
  • Use f-string instead of many +: f"Hello {name}"
  • Use "-" * 20 to print a line fast.
  • == check same value. Big and small letter are different.
  • Ignore case? Use .lower(): "Apple".lower() == "apple"

โ“ Any Question?

Tell us in comment below! ๐Ÿ‘‡

#Python #Operators #String #List #LearnPython #Beginner


๐Ÿ“š Related Post

โ† Back to python
Share: