5 cách đảo ngược chuỗi trong Python đơn giản và hiệu quả
3 phút đọc15/09/2025
Về tác giả
I have over 5 years of experience writing technical documentation for tech products, making them accessible and user-friendly. My focus is always on providing clear and precise information.
@#@
Tôi đã có hơn 5 năm kinh nghiệm viết tài liệu kỹ thuật cho các sản phẩm công nghệ, giúp người dùng dễ dàng tiếp cận và sử dụng. Tôi luôn tập trung vào việc cung cấp thông tin chính xác và dễ hiểu.
Cập nhật thông tin mới nhấtNhận các thông tin mới nhất về mối đe dọa, báo cáo an ninh mạng từ CyStack về hòm thư điện tử của bạn
{"success":true,"head":"<title>5 cách đảo ngược chuỗi trong Python đơn giản và hiệu quả - CyStack Tutorial</title>\n<meta name=\"description\" content=\"Bài viết này sẽ giới thiệu 5 cách đảo ngược chuỗi trong Python từ đơn giản đến tối ưu, giúp bạn dễ dàng áp dụng trong thực tế.\"/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-video-preview:-1, max-image-preview:large\"/>\n<link rel=\"canonical\" href=\"https://blog.cystack.org/tutorial/vi/2025/09/15/5-cach-dao-nguoc-chuoi-trong-python/\" />\n<meta property=\"og:locale\" content=\"en_US\" />\n<meta property=\"og:type\" content=\"article\" />\n<meta property=\"og:title\" content=\"5 cách đảo ngược chuỗi trong Python đơn giản và hiệu quả - CyStack Tutorial\" />\n<meta property=\"og:description\" content=\"Bài viết này sẽ giới thiệu 5 cách đảo ngược chuỗi trong Python từ đơn giản đến tối ưu, giúp bạn dễ dàng áp dụng trong thực tế.\" />\n<meta property=\"og:url\" content=\"https://blog.cystack.org/tutorial/vi/2025/09/15/5-cach-dao-nguoc-chuoi-trong-python/\" />\n<meta property=\"og:site_name\" content=\"CyStack Tutorial\" />\n<meta property=\"article:tag\" content=\"vi\" />\n<meta property=\"article:section\" content=\"Python\" />\n<meta property=\"og:updated_time\" content=\"2026-08-03T18:17:56+07:00\" />\n<meta property=\"og:image\" content=\"https://s2.cystack.net/tutorial/15111937/Frame-10-21.png\" />\n<meta property=\"og:image:secure_url\" content=\"https://s2.cystack.net/tutorial/15111937/Frame-10-21.png\" />\n<meta property=\"og:image:width\" content=\"1200\" />\n<meta property=\"og:image:height\" content=\"630\" />\n<meta property=\"og:image:alt\" content=\"đảo ngược chuỗi trong Python\" />\n<meta property=\"og:image:type\" content=\"image/png\" />\n<meta property=\"article:published_time\" content=\"2025-09-15T11:20:43+07:00\" />\n<meta property=\"article:modified_time\" content=\"2026-08-03T18:17:56+07:00\" />\n<meta name=\"twitter:card\" content=\"summary_large_image\" />\n<meta name=\"twitter:title\" content=\"5 cách đảo ngược chuỗi trong Python đơn giản và hiệu quả - CyStack Tutorial\" />\n<meta name=\"twitter:description\" content=\"Bài viết này sẽ giới thiệu 5 cách đảo ngược chuỗi trong Python từ đơn giản đến tối ưu, giúp bạn dễ dàng áp dụng trong thực tế.\" />\n<meta name=\"twitter:image\" content=\"https://s2.cystack.net/tutorial/15111937/Frame-10-21.png\" />\n<meta name=\"twitter:label1\" content=\"Written by\" />\n<meta name=\"twitter:data1\" content=\"Chris Pham\" />\n<meta name=\"twitter:label2\" content=\"Time to read\" />\n<meta name=\"twitter:data2\" content=\"3 minutes\" />\n<script type=\"application/ld+json\" class=\"rank-math-schema\">{\"@context\":\"https://schema.org\",\"@graph\":[{\"@type\":[\"Person\",\"Organization\"],\"@id\":\"https://blog.cystack.org/tutorial/#person\",\"name\":\"CyStack Tutorial\"},{\"@type\":\"WebSite\",\"@id\":\"https://blog.cystack.org/tutorial/#website\",\"url\":\"https://blog.cystack.org/tutorial\",\"name\":\"CyStack Tutorial\",\"publisher\":{\"@id\":\"https://blog.cystack.org/tutorial/#person\"},\"inLanguage\":\"en-US\"},{\"@type\":\"ImageObject\",\"@id\":\"https://s2.cystack.net/tutorial/15111937/Frame-10-21.png\",\"url\":\"https://s2.cystack.net/tutorial/15111937/Frame-10-21.png\",\"width\":\"1200\",\"height\":\"630\",\"caption\":\"\\u0111\\u1ea3o ng\\u01b0\\u1ee3c chu\\u1ed7i trong Python\",\"inLanguage\":\"en-US\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https://blog.cystack.org/tutorial/vi/2025/09/15/5-cach-dao-nguoc-chuoi-trong-python/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":\"1\",\"item\":{\"@id\":\"https://blog.cystack.org/tutorial\",\"name\":\"Home\"}},{\"@type\":\"ListItem\",\"position\":\"2\",\"item\":{\"@id\":\"https://blog.cystack.org/tutorial/vi/category/python/\",\"name\":\"Python\"}},{\"@type\":\"ListItem\",\"position\":\"3\",\"item\":{\"@id\":\"https://blog.cystack.org/tutorial/vi/2025/09/15/5-cach-dao-nguoc-chuoi-trong-python/\",\"name\":\"5 c\\u00e1ch \\u0111\\u1ea3o ng\\u01b0\\u1ee3c chu\\u1ed7i trong Python \\u0111\\u01a1n gi\\u1ea3n v\\u00e0 hi\\u1ec7u qu\\u1ea3\"}}]},{\"@type\":\"WebPage\",\"@id\":\"https://blog.cystack.org/tutorial/vi/2025/09/15/5-cach-dao-nguoc-chuoi-trong-python/#webpage\",\"url\":\"https://blog.cystack.org/tutorial/vi/2025/09/15/5-cach-dao-nguoc-chuoi-trong-python/\",\"name\":\"5 c\\u00e1ch \\u0111\\u1ea3o ng\\u01b0\\u1ee3c chu\\u1ed7i trong Python \\u0111\\u01a1n gi\\u1ea3n v\\u00e0 hi\\u1ec7u qu\\u1ea3 - CyStack Tutorial\",\"datePublished\":\"2025-09-15T11:20:43+07:00\",\"dateModified\":\"2026-08-03T18:17:56+07:00\",\"isPartOf\":{\"@id\":\"https://blog.cystack.org/tutorial/#website\"},\"primaryImageOfPage\":{\"@id\":\"https://s2.cystack.net/tutorial/15111937/Frame-10-21.png\"},\"inLanguage\":\"en-US\",\"breadcrumb\":{\"@id\":\"https://blog.cystack.org/tutorial/vi/2025/09/15/5-cach-dao-nguoc-chuoi-trong-python/#breadcrumb\"}},{\"@type\":\"Person\",\"@id\":\"https://blog.cystack.org/tutorial/author/codeweaver/\",\"name\":\"Chris Pham\",\"url\":\"https://blog.cystack.org/tutorial/author/codeweaver/\",\"image\":{\"@type\":\"ImageObject\",\"@id\":\"https://secure.gravatar.com/avatar/7a3584c941fa360ac061b2a8f15684ced7b92daa1f09481be135a684d989e9b9?s=96&d=mm&r=g\",\"url\":\"https://secure.gravatar.com/avatar/7a3584c941fa360ac061b2a8f15684ced7b92daa1f09481be135a684d989e9b9?s=96&d=mm&r=g\",\"caption\":\"Chris Pham\",\"inLanguage\":\"en-US\"}},{\"@type\":\"BlogPosting\",\"headline\":\"5 c\\u00e1ch \\u0111\\u1ea3o ng\\u01b0\\u1ee3c chu\\u1ed7i trong Python \\u0111\\u01a1n gi\\u1ea3n v\\u00e0 hi\\u1ec7u qu\\u1ea3 - CyStack Tutorial\",\"keywords\":\"\\u0111\\u1ea3o ng\\u01b0\\u1ee3c chu\\u1ed7i trong Python,Python\",\"datePublished\":\"2025-09-15T11:20:43+07:00\",\"dateModified\":\"2026-08-03T18:17:56+07:00\",\"articleSection\":\"Python\",\"author\":{\"@id\":\"https://blog.cystack.org/tutorial/author/codeweaver/\",\"name\":\"Chris Pham\"},\"publisher\":{\"@id\":\"https://blog.cystack.org/tutorial/#person\"},\"description\":\"B\\u00e0i vi\\u1ebft n\\u00e0y s\\u1ebd gi\\u1edbi thi\\u1ec7u 5 c\\u00e1ch \\u0111\\u1ea3o ng\\u01b0\\u1ee3c chu\\u1ed7i trong Python t\\u1eeb \\u0111\\u01a1n gi\\u1ea3n \\u0111\\u1ebfn t\\u1ed1i \\u01b0u, gi\\u00fap b\\u1ea1n d\\u1ec5 d\\u00e0ng \\u00e1p d\\u1ee5ng trong th\\u1ef1c t\\u1ebf.\",\"name\":\"5 c\\u00e1ch \\u0111\\u1ea3o ng\\u01b0\\u1ee3c chu\\u1ed7i trong Python \\u0111\\u01a1n gi\\u1ea3n v\\u00e0 hi\\u1ec7u qu\\u1ea3 - CyStack Tutorial\",\"@id\":\"https://blog.cystack.org/tutorial/vi/2025/09/15/5-cach-dao-nguoc-chuoi-trong-python/#richSnippet\",\"isPartOf\":{\"@id\":\"https://blog.cystack.org/tutorial/vi/2025/09/15/5-cach-dao-nguoc-chuoi-trong-python/#webpage\"},\"image\":{\"@id\":\"https://s2.cystack.net/tutorial/15111937/Frame-10-21.png\"},\"inLanguage\":\"en-US\",\"mainEntityOfPage\":{\"@id\":\"https://blog.cystack.org/tutorial/vi/2025/09/15/5-cach-dao-nguoc-chuoi-trong-python/#webpage\"}}]}</script>\n"}
Đảo ngược chuỗi là một trong những thao tác cơ bản nhưng hữu ích trong lập trình Python.
Bài viết này sẽ giới thiệu 5 cách đảo ngược chuỗi trong Python từ đơn giản đến tối ưu, giúp bạn dễ dàng áp dụng trong thực tế.

Cách đảo ngược chuỗi trong Python?
Một số cách đảo ngược chuỗi phổ biến như sau:
- Dùng slicing để tạo bản sao đảo ngược của chuỗi
- Dùng vòng lặp
for và nối các ký tự theo thứ tự ngược
- Dùng vòng lặp
while để duyệt ký tự chuỗi theo thứ tự ngược và nối chúng
- Dùng hàm
join() của chuỗi kết hợp với iterator reversed()
- Tạo list từ chuỗi rồi gọi hàm
reverse() của list
- Dùng đệ quy
1.1) Đảo ngược chuỗi Python bằng Slicing
def reverse_slicing(s):
return s[::-1]
input_str = 'ABç∂EF'
if name == "main":
print('Reverse String using slicing =', reverse_slicing(input_str))
Khi chạy đoạn script Python trên, đầu ra sẽ là:
Reverse String using slicing = FE∂çBA
1.2) Đảo ngược chuỗi bằng For Loop
def reverse_for_loop(s):
s1 = ''
for c in s:
s1 = c + s1 # appending chars in reverse order
return s1
input_str = 'ABç∂EF'
if name == "main":
print('Reverse String using for loop =', reverse_for_loop(input_str))
Đầu ra: Reverse String using for loop = FE∂çBA
1.3) Đảo ngược chuỗi bằng While Loop
def reverse_while_loop(s):
s1 = ''
length = len(s) - 1
while length >= 0:
s1 = s1 + s[length]
length = length - 1
return s1
input_str = 'ABç∂EF'
if name == "main":
print('Reverse String using while loop =', reverse_while_loop(input_str))
1.4) Đảo ngược chuỗi bằng join() và reversed()
def reverse_join_reversed_iter(s):
s1 = ''.join(reversed(s))
return s1
1.5) Đảo ngược chuỗi Python bằng List reverse()
def reverse_list(s):
temp_list = list(s)
temp_list.reverse()
return ''.join(temp_list)
1.6) Đảo ngược chuỗi Python bằng Đệ quy
def reverse_recursion(s):
if len(s) == 0:
return s
else:
return reverse_recursion(s[1:]) + s[0]
Cách tốt nhất để đảo ngược chuỗi trong Python
Có nhiều thuật toán để đảo ngược chuỗi, trong đó 6 cách đã được giới thiệu ở trên. Để xác định phương pháp tối ưu, chúng ta có thể dùng module timeit chạy lặp lại các hàm này nhiều lần và lấy thời gian trung bình để đánh giá.
Tất cả các hàm trên được lưu trong script Python có tên string_reverse.py. Mỗi hàm được chạy 100.000 lần bằng module timeit và lấy trung bình của 5 lần chạy nhanh nhất.
$ python3.7 -m timeit --number 100000 --unit usec 'import string_reverse' 'string_reverse.reverse_slicing("ABç∂EF"*10)'
100000 loops, best of 5: 0.449 usec per loop
$ python3.7 -m timeit --number 100000 --unit usec 'import string_reverse' 'string_reverse.reverse_list("ABç∂EF"*10)'
100000 loops, best of 5: 2.46 usec per loop
$ python3.7 -m timeit --number 100000 --unit usec 'import string_reverse' 'string_reverse.reverse_join_reversed_iter("ABç∂EF"*10)'
100000 loops, best of 5: 2.49 usec per loop
$ python3.7 -m timeit --number 100000 --unit usec 'import string_reverse' 'string_reverse.reverse_for_loop("ABç∂EF"*10)'
100000 loops, best of 5: 5.5 usec per loop
$ python3.7 -m timeit --number 100000 --unit usec 'import string_reverse' 'string_reverse.reverse_while_loop("ABç∂EF"*10)'
100000 loops, best of 5: 9.4 usec per loop
$ python3.7 -m timeit --number 100000 --unit usec 'import string_reverse' 'string_reverse.reverse_recursion("ABç∂EF"*10)'
100000 loops, best of 5: 24.3 usec per loop

Bảng dưới đây trình bày kết quả và mức độ chậm của từng thuật toán so với phương pháp nhanh nhất.
Tóm tắt
Chúng ta nên dùng slicing để đảo ngược chuỗi trong Python. Cú pháp rất ngắn gọn, không cần tự viết logic xử lý. Ngoài ra, đây cũng là cách nhanh nhất trong kết quả kiểm thử trên.
Bạn có thể xem toàn bộ script Python và thêm ví dụ khác trên GitHub Repository của chúng tôi.
Tham khảo
- Tài liệu API
reversed()
- Tài liệu API
str.join()