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
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
"-" * 20to 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