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! ๐Ÿ‘‹ You know + and * for number. But 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)


โž• Operator + (Join)

Join two string or two list into one.

# string
first = "Hello"
second = "World"
print(first + " " + second)   # Hello World

# list
a = [1, 2]
b = [3, 4]
print(a + b)                  # [1, 2, 3, 4]

Cannot join different type:

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

โœ–๏ธ Operator * (Repeat)

Repeat string or list many time.

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

Good for make line or default list fast.


๐Ÿ” 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
print("java" in "python")       # False

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

Good for 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] == [1, 2])      # True
print([1, 2] == [2, 1])      # False (order matter)

Compare string by alphabet order:

print("apple" < "banana")     # True
print("a" < "B")             # False  (big letter come first)

๐Ÿ”— 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(len([1, 2, 3]))      # 3
print(min([5, 2, 8]))      # 2
print(max("abc"))          # c

โŒ Common Error

String cannot change by index:

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

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 (immutable). List can change (mutable).
  • Use + to 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 line quick.
  • Use in to check before remove() from list. No error!
  • == check same value. Big and small letter are different.
  • Want 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: