{"id":1474,"date":"2025-09-27T20:36:00","date_gmt":"2025-09-27T13:36:00","guid":{"rendered":"https:\/\/kienthucmo.com\/?p=1474"},"modified":"2025-11-05T23:32:52","modified_gmt":"2025-11-05T16:32:52","slug":"tim-hieu-string-trong-python-khai-niem-dac-diem-va-ung-dung","status":"publish","type":"post","link":"https:\/\/kienthucmo.com\/vi\/tim-hieu-string-trong-python-khai-niem-dac-diem-va-ung-dung\/","title":{"rendered":"T\u00ecm hi\u1ec3u String trong Python: Kh\u00e1i ni\u1ec7m, \u0110\u1eb7c \u0111i\u1ec3m v\u00e0 \u1ee8ng d\u1ee5ng"},"content":{"rendered":"\n<p>Strings (chu\u1ed7i k\u00fd t\u1ef1) l\u00e0 m\u1ed9t trong nh\u1eefng ki\u1ec3u d\u1eef li\u1ec7u c\u01a1 b\u1ea3n v\u00e0 \u0111\u01b0\u1ee3c d\u00f9ng nhi\u1ec1u nh\u1ea5t trong l\u1eadp tr\u00ecnh \u2014 t\u1eeb in ra m\u1ed9t c\u00e2u <code>\"Hello, world!\"<\/code>, \u0111\u1ecdc\/ghi file, x\u1eed l\u00fd d\u1eef li\u1ec7u v\u0103n b\u1ea3n \u0111\u1ebfn giao ti\u1ebfp v\u1edbi API hay l\u00e0m log. Hi\u1ec3u r\u00f5 b\u1ea3n ch\u1ea5t c\u1ee7a string trong Python gi\u00fap b\u1ea1n tr\u00e1nh \u0111\u01b0\u1ee3c nhi\u1ec1u l\u1ed7i kh\u00f3 ch\u1ecbu (nh\u1ea5t l\u00e0 li\u00ean quan \u0111\u1ebfn encoding v\u00e0 lo\u1ea1i <code>bytes<\/code> vs <code>str<\/code>), \u0111\u1ed3ng th\u1eddi \u0111\u1eb7t n\u1ec1n t\u1ea3ng v\u1eefng ch\u1eafc cho c\u00e1c k\u1ef9 thu\u1eadt x\u1eed l\u00fd chu\u1ed7i n\u00e2ng cao (regex, normalization, tokenization\u2026).<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">1. String trong Python l\u00e0 g\u00ec?<\/h2>\n\n\n\n<ol class=\"wp-block-list\">\n<li><\/li>\n<\/ol>\n\n\n\n<p>String trong Python l\u00e0 m\u1ed9t d\u00e3y k\u00fd t\u1ef1 (sequence of characters). Python 3 s\u1eed d\u1ee5ng <strong>Unicode<\/strong> cho string, t\u1ee9c l\u00e0 c\u00f3 th\u1ec3 bi\u1ec3u di\u1ec5n nhi\u1ec1u lo\u1ea1i ng\u00f4n ng\u1eef kh\u00e1c nhau, bao g\u1ed3m c\u1ea3 ti\u1ebfng Vi\u1ec7t c\u00f3 d\u1ea5u v\u00e0 emoji.<\/p>\n\n\n\n<p>V\u00ed d\u1ee5 khai b\u00e1o string:<\/p>\n\n\n\n<div class=\"wp-block-kevinbatdorf-code-block-pro\" data-code-block-pro-font-family=\"Code-Pro-JetBrains-Mono\" style=\"font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)\"><span style=\"display:block;padding:16px 0 0 16px;margin-bottom:-1px;width:100%;text-align:left;background-color:#2e3440ff\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"54\" height=\"14\" viewBox=\"0 0 54 14\"><g fill=\"none\" fill-rule=\"evenodd\" transform=\"translate(1 1)\"><circle cx=\"6\" cy=\"6\" r=\"6\" fill=\"#FF5F56\" stroke=\"#E0443E\" stroke-width=\".5\"><\/circle><circle cx=\"26\" cy=\"6\" r=\"6\" fill=\"#FFBD2E\" stroke=\"#DEA123\" stroke-width=\".5\"><\/circle><circle cx=\"46\" cy=\"6\" r=\"6\" fill=\"#27C93F\" stroke=\"#1AAB29\" stroke-width=\".5\"><\/circle><\/g><\/svg><\/span><span role=\"button\" tabindex=\"0\" style=\"color:#d8dee9ff;display:none\" aria-label=\"Copy\" class=\"code-block-pro-copy-button\"><pre class=\"code-block-pro-copy-button-pre\" aria-hidden=\"true\"><textarea class=\"code-block-pro-copy-button-textarea\" tabindex=\"-1\" aria-hidden=\"true\" readonly>a = 'KienThucMo'\nb = \"openknowledge\"\nc = \"\"\"This is a\nmulti-line\nstring\"\"\"<\/textarea><\/pre><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" style=\"width:24px;height:24px\" fill=\"none\" viewBox=\"0 0 24 24\" stroke=\"currentColor\" stroke-width=\"2\"><path class=\"with-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4\"><\/path><path class=\"without-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2\"><\/path><\/svg><\/span><pre class=\"shiki nord\" style=\"background-color: #2e3440ff\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color: #D8DEE9FF\">a <\/span><span style=\"color: #81A1C1\">=<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #ECEFF4\">&#39;<\/span><span style=\"color: #A3BE8C\">KienThucMo<\/span><span style=\"color: #ECEFF4\">&#39;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D8DEE9FF\">b <\/span><span style=\"color: #81A1C1\">=<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #ECEFF4\">&quot;<\/span><span style=\"color: #A3BE8C\">openknowledge<\/span><span style=\"color: #ECEFF4\">&quot;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D8DEE9FF\">c <\/span><span style=\"color: #81A1C1\">=<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #ECEFF4\">&quot;&quot;&quot;<\/span><span style=\"color: #A3BE8C\">This is a<\/span><\/span>\n<span class=\"line\"><span style=\"color: #A3BE8C\">multi-line<\/span><\/span>\n<span class=\"line\"><span style=\"color: #A3BE8C\">string<\/span><span style=\"color: #ECEFF4\">&quot;&quot;&quot;<\/span><\/span><\/code><\/pre><\/div>\n\n\n\n<p>\u0110i\u1ec3m c\u1ea7n nh\u1edb:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>C\u00f3 th\u1ec3 d\u00f9ng nh\u00e1y \u0111\u01a1n <code>'...'<\/code> ho\u1eb7c nh\u00e1y k\u00e9p <code>\"...\"<\/code> \u0111\u1ec3 t\u1ea1o string.<\/li>\n\n\n\n<li>D\u00f9ng ba d\u1ea5u nh\u00e1y <code>'''...'''<\/code> ho\u1eb7c <code>\"\"\"...\"\"\"<\/code> \u0111\u1ec3 t\u1ea1o string nhi\u1ec1u d\u00f2ng.<\/li>\n\n\n\n<li>String r\u1ed7ng \u0111\u01b0\u1ee3c vi\u1ebft l\u00e0 <code>\"\"<\/code>.<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">2. \u0110\u1eb7c \u0111i\u1ec3m quan tr\u1ecdng c\u1ee7a string<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">2.1 String l\u00e0 <strong>immutable<\/strong> (kh\u00f4ng thay \u0111\u1ed5i \u0111\u01b0\u1ee3c)<\/h3>\n\n\n\n<p>M\u1ed9t khi string \u0111\u01b0\u1ee3c t\u1ea1o, b\u1ea1n kh\u00f4ng th\u1ec3 thay \u0111\u1ed5i tr\u1ef1c ti\u1ebfp n\u1ed9i dung c\u1ee7a n\u00f3. N\u1ebfu c\u1ea7n \u201cthay \u0111\u1ed5i\u201d, th\u1ef1c ch\u1ea5t b\u1ea1n \u0111ang t\u1ea1o ra m\u1ed9t string m\u1edbi.<\/p>\n\n\n\n<p>V\u00ed d\u1ee5:<\/p>\n\n\n\n<div class=\"wp-block-kevinbatdorf-code-block-pro\" data-code-block-pro-font-family=\"Code-Pro-JetBrains-Mono\" style=\"font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)\"><span style=\"display:block;padding:16px 0 0 16px;margin-bottom:-1px;width:100%;text-align:left;background-color:#2e3440ff\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"54\" height=\"14\" viewBox=\"0 0 54 14\"><g fill=\"none\" fill-rule=\"evenodd\" transform=\"translate(1 1)\"><circle cx=\"6\" cy=\"6\" r=\"6\" fill=\"#FF5F56\" stroke=\"#E0443E\" stroke-width=\".5\"><\/circle><circle cx=\"26\" cy=\"6\" r=\"6\" fill=\"#FFBD2E\" stroke=\"#DEA123\" stroke-width=\".5\"><\/circle><circle cx=\"46\" cy=\"6\" r=\"6\" fill=\"#27C93F\" stroke=\"#1AAB29\" stroke-width=\".5\"><\/circle><\/g><\/svg><\/span><span role=\"button\" tabindex=\"0\" style=\"color:#d8dee9ff;display:none\" aria-label=\"Copy\" class=\"code-block-pro-copy-button\"><pre class=\"code-block-pro-copy-button-pre\" aria-hidden=\"true\"><textarea class=\"code-block-pro-copy-button-textarea\" tabindex=\"-1\" aria-hidden=\"true\" readonly>word = \"hello\"\n# word&#91;0&#93; = \"H\"  # Error: strings are immutable\nword = \"H\" + word&#91;1:&#93;  # Create a new string<\/textarea><\/pre><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" style=\"width:24px;height:24px\" fill=\"none\" viewBox=\"0 0 24 24\" stroke=\"currentColor\" stroke-width=\"2\"><path class=\"with-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4\"><\/path><path class=\"without-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2\"><\/path><\/svg><\/span><pre class=\"shiki nord\" style=\"background-color: #2e3440ff\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color: #D8DEE9FF\">word <\/span><span style=\"color: #81A1C1\">=<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #ECEFF4\">&quot;<\/span><span style=\"color: #A3BE8C\">hello<\/span><span style=\"color: #ECEFF4\">&quot;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #616E88\"># word&#91;0&#93; = &quot;H&quot;  # Error: strings are immutable<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D8DEE9FF\">word <\/span><span style=\"color: #81A1C1\">=<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #ECEFF4\">&quot;<\/span><span style=\"color: #A3BE8C\">H<\/span><span style=\"color: #ECEFF4\">&quot;<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #81A1C1\">+<\/span><span style=\"color: #D8DEE9FF\"> word<\/span><span style=\"color: #ECEFF4\">&#91;<\/span><span style=\"color: #B48EAD\">1<\/span><span style=\"color: #ECEFF4\">:&#93;<\/span><span style=\"color: #D8DEE9FF\">  <\/span><span style=\"color: #616E88\"># Create a new string<\/span><\/span><\/code><\/pre><\/div>\n\n\n\n<p>\u0110i\u1ec1u n\u00e0y gi\u00fap string an to\u00e0n khi s\u1eed d\u1ee5ng l\u00e0m <strong>key trong dictionary<\/strong> ho\u1eb7c khi chia s\u1ebb d\u1eef li\u1ec7u, nh\u01b0ng c\u0169ng \u0111\u1ed3ng ngh\u0129a c\u1ea7n l\u01b0u \u00fd v\u1ec1 hi\u1ec7u n\u0103ng khi thao t\u00e1c v\u1edbi chu\u1ed7i l\u1edbn.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">2.2 String l\u00e0 m\u1ed9t <strong>sequence<\/strong><\/h3>\n\n\n\n<p>V\u00ec l\u00e0 sequence, string c\u00f3 nh\u1eefng \u0111\u1eb7c t\u00ednh nh\u01b0:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>C\u00f3 \u0111\u1ed9 d\u00e0i: <code>len(\"Hello\")<\/code> tr\u1ea3 v\u1ec1 <code>5<\/code>.<\/li>\n\n\n\n<li>C\u00f3 th\u1ec3 duy\u1ec7t t\u1eebng k\u00fd t\u1ef1 b\u1eb1ng v\u00f2ng l\u1eb7p: <code>for ch in \"abc\": print(ch)<\/code><\/li>\n\n\n\n<li>C\u00f3 th\u1ec3 ki\u1ec3m tra t\u1ed3n t\u1ea1i: <code>\"a\" in \"abc\"<\/code> \u2192 <code>True<\/code>.<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">3. Nh\u1eefng l\u1ed7i th\u01b0\u1eddng g\u1eb7p v\u1edbi string<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">3.1 UnicodeEncodeError \/ UnicodeDecodeError<\/h3>\n\n\n\n<p>\u0110\u00e2y l\u00e0 l\u1ed7i ph\u1ed5 bi\u1ebfn nh\u1ea5t khi l\u00e0m vi\u1ec7c v\u1edbi d\u1eef li\u1ec7u v\u0103n b\u1ea3n \u0111a ng\u00f4n ng\u1eef. Nguy\u00ean nh\u00e2n th\u01b0\u1eddng l\u00e0 b\u1ea1n \u0111\u1ecdc ho\u1eb7c ghi file m\u00e0 kh\u00f4ng ch\u1ec9 \u0111\u1ecbnh \u0111\u00fang <strong>encoding<\/strong>. V\u00ed d\u1ee5, m\u1ed9t file l\u01b0u b\u1eb1ng UTF-8 nh\u01b0ng b\u1ea1n m\u1edf m\u1eb7c \u0111\u1ecbnh l\u1ea1i theo Windows-1252, Python s\u1ebd b\u00e1o l\u1ed7i.<\/p>\n\n\n\n<div class=\"wp-block-kevinbatdorf-code-block-pro\" data-code-block-pro-font-family=\"Code-Pro-JetBrains-Mono\" style=\"font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)\"><span style=\"display:block;padding:16px 0 0 16px;margin-bottom:-1px;width:100%;text-align:left;background-color:#2e3440ff\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"54\" height=\"14\" viewBox=\"0 0 54 14\"><g fill=\"none\" fill-rule=\"evenodd\" transform=\"translate(1 1)\"><circle cx=\"6\" cy=\"6\" r=\"6\" fill=\"#FF5F56\" stroke=\"#E0443E\" stroke-width=\".5\"><\/circle><circle cx=\"26\" cy=\"6\" r=\"6\" fill=\"#FFBD2E\" stroke=\"#DEA123\" stroke-width=\".5\"><\/circle><circle cx=\"46\" cy=\"6\" r=\"6\" fill=\"#27C93F\" stroke=\"#1AAB29\" stroke-width=\".5\"><\/circle><\/g><\/svg><\/span><span role=\"button\" tabindex=\"0\" style=\"color:#d8dee9ff;display:none\" aria-label=\"Copy\" class=\"code-block-pro-copy-button\"><pre class=\"code-block-pro-copy-button-pre\" aria-hidden=\"true\"><textarea class=\"code-block-pro-copy-button-textarea\" tabindex=\"-1\" aria-hidden=\"true\" readonly># Sai encoding -> c\u00f3 th\u1ec3 g\u00e2y l\u1ed7i UnicodeDecodeError\nwith open(\"data.txt\", \"r\") as file:\n    text = file.read()\n\n# \u0110\u00fang encoding -> an to\u00e0n h\u01a1n\nwith open(\"data.txt\", \"r\", encoding=\"utf-8\") as file:\n    text = file.read()<\/textarea><\/pre><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" style=\"width:24px;height:24px\" fill=\"none\" viewBox=\"0 0 24 24\" stroke=\"currentColor\" stroke-width=\"2\"><path class=\"with-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4\"><\/path><path class=\"without-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2\"><\/path><\/svg><\/span><pre class=\"shiki nord\" style=\"background-color: #2e3440ff\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color: #616E88\"># Sai encoding -&gt; c\u00f3 th\u1ec3 g\u00e2y l\u1ed7i UnicodeDecodeError<\/span><\/span>\n<span class=\"line\"><span style=\"color: #81A1C1\">with<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #88C0D0\">open<\/span><span style=\"color: #ECEFF4\">(<\/span><span style=\"color: #ECEFF4\">&quot;<\/span><span style=\"color: #A3BE8C\">data.txt<\/span><span style=\"color: #ECEFF4\">&quot;<\/span><span style=\"color: #ECEFF4\">,<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #ECEFF4\">&quot;<\/span><span style=\"color: #A3BE8C\">r<\/span><span style=\"color: #ECEFF4\">&quot;<\/span><span style=\"color: #ECEFF4\">)<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #81A1C1\">as<\/span><span style=\"color: #D8DEE9FF\"> file<\/span><span style=\"color: #ECEFF4\">:<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D8DEE9FF\">    text <\/span><span style=\"color: #81A1C1\">=<\/span><span style=\"color: #D8DEE9FF\"> file<\/span><span style=\"color: #ECEFF4\">.<\/span><span style=\"color: #88C0D0\">read<\/span><span style=\"color: #ECEFF4\">()<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #616E88\"># \u0110\u00fang encoding -&gt; an to\u00e0n h\u01a1n<\/span><\/span>\n<span class=\"line\"><span style=\"color: #81A1C1\">with<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #88C0D0\">open<\/span><span style=\"color: #ECEFF4\">(<\/span><span style=\"color: #ECEFF4\">&quot;<\/span><span style=\"color: #A3BE8C\">data.txt<\/span><span style=\"color: #ECEFF4\">&quot;<\/span><span style=\"color: #ECEFF4\">,<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #ECEFF4\">&quot;<\/span><span style=\"color: #A3BE8C\">r<\/span><span style=\"color: #ECEFF4\">&quot;<\/span><span style=\"color: #ECEFF4\">,<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #D8DEE9\">encoding<\/span><span style=\"color: #81A1C1\">=<\/span><span style=\"color: #ECEFF4\">&quot;<\/span><span style=\"color: #A3BE8C\">utf-8<\/span><span style=\"color: #ECEFF4\">&quot;<\/span><span style=\"color: #ECEFF4\">)<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #81A1C1\">as<\/span><span style=\"color: #D8DEE9FF\"> file<\/span><span style=\"color: #ECEFF4\">:<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D8DEE9FF\">    text <\/span><span style=\"color: #81A1C1\">=<\/span><span style=\"color: #D8DEE9FF\"> file<\/span><span style=\"color: #ECEFF4\">.<\/span><span style=\"color: #88C0D0\">read<\/span><span style=\"color: #ECEFF4\">()<\/span><\/span><\/code><\/pre><\/div>\n\n\n\n<h3 class=\"wp-block-heading\">3.2 Tr\u1ed9n <code>str<\/code> v\u00e0 <code>bytes<\/code><\/h3>\n\n\n\n<p>Trong Python 3, <code>str<\/code> v\u00e0 <code>bytes<\/code> l\u00e0 hai ki\u1ec3u kh\u00e1c nhau. N\u1ebfu b\u1ea1n gh\u00e9p ch\u00fang tr\u1ef1c ti\u1ebfp, ch\u01b0\u01a1ng tr\u00ecnh s\u1ebd b\u00e1o l\u1ed7i:<\/p>\n\n\n\n<div class=\"wp-block-kevinbatdorf-code-block-pro\" data-code-block-pro-font-family=\"Code-Pro-JetBrains-Mono\" style=\"font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)\"><span style=\"display:block;padding:16px 0 0 16px;margin-bottom:-1px;width:100%;text-align:left;background-color:#2e3440ff\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"54\" height=\"14\" viewBox=\"0 0 54 14\"><g fill=\"none\" fill-rule=\"evenodd\" transform=\"translate(1 1)\"><circle cx=\"6\" cy=\"6\" r=\"6\" fill=\"#FF5F56\" stroke=\"#E0443E\" stroke-width=\".5\"><\/circle><circle cx=\"26\" cy=\"6\" r=\"6\" fill=\"#FFBD2E\" stroke=\"#DEA123\" stroke-width=\".5\"><\/circle><circle cx=\"46\" cy=\"6\" r=\"6\" fill=\"#27C93F\" stroke=\"#1AAB29\" stroke-width=\".5\"><\/circle><\/g><\/svg><\/span><span role=\"button\" tabindex=\"0\" style=\"color:#d8dee9ff;display:none\" aria-label=\"Copy\" class=\"code-block-pro-copy-button\"><pre class=\"code-block-pro-copy-button-pre\" aria-hidden=\"true\"><textarea class=\"code-block-pro-copy-button-textarea\" tabindex=\"-1\" aria-hidden=\"true\" readonly>\"abc\" + b\"def\"   # TypeError<\/textarea><\/pre><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" style=\"width:24px;height:24px\" fill=\"none\" viewBox=\"0 0 24 24\" stroke=\"currentColor\" stroke-width=\"2\"><path class=\"with-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4\"><\/path><path class=\"without-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2\"><\/path><\/svg><\/span><pre class=\"shiki nord\" style=\"background-color: #2e3440ff\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color: #ECEFF4\">&quot;<\/span><span style=\"color: #A3BE8C\">abc<\/span><span style=\"color: #ECEFF4\">&quot;<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #81A1C1\">+<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #81A1C1\">b<\/span><span style=\"color: #ECEFF4\">&quot;<\/span><span style=\"color: #A3BE8C\">def<\/span><span style=\"color: #ECEFF4\">&quot;<\/span><span style=\"color: #D8DEE9FF\">   <\/span><span style=\"color: #616E88\"># TypeError<\/span><\/span><\/code><\/pre><\/div>\n\n\n\n<p>Mu\u1ed1n k\u1ebft h\u1ee3p, b\u1ea1n ph\u1ea3i <strong>encode str th\u00e0nh bytes<\/strong> ho\u1eb7c <strong>decode bytes th\u00e0nh str<\/strong> tr\u01b0\u1edbc.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">3.3 Kho\u1ea3ng tr\u1eafng v\u00f4 h\u00ecnh<\/h3>\n\n\n\n<p>M\u1ed9t chu\u1ed7i <code>\"hello\"<\/code> v\u00e0 <code>\"hello \"<\/code> (c\u00f3 kho\u1ea3ng tr\u1eafng \u1edf cu\u1ed1i) tr\u00f4ng r\u1ea5t gi\u1ed1ng nhau, nh\u01b0ng khi so s\u00e1nh ch\u00fang l\u1ea1i kh\u00e1c bi\u1ec7t. T\u01b0\u01a1ng t\u1ef1, k\u00fd t\u1ef1 xu\u1ed1ng d\u00f2ng <code>\\n<\/code> c\u0169ng th\u01b0\u1eddng g\u00e2y nh\u1ea7m l\u1eabn.<\/p>\n\n\n\n<div class=\"wp-block-kevinbatdorf-code-block-pro\" data-code-block-pro-font-family=\"Code-Pro-JetBrains-Mono\" style=\"font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)\"><span style=\"display:block;padding:16px 0 0 16px;margin-bottom:-1px;width:100%;text-align:left;background-color:#2e3440ff\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"54\" height=\"14\" viewBox=\"0 0 54 14\"><g fill=\"none\" fill-rule=\"evenodd\" transform=\"translate(1 1)\"><circle cx=\"6\" cy=\"6\" r=\"6\" fill=\"#FF5F56\" stroke=\"#E0443E\" stroke-width=\".5\"><\/circle><circle cx=\"26\" cy=\"6\" r=\"6\" fill=\"#FFBD2E\" stroke=\"#DEA123\" stroke-width=\".5\"><\/circle><circle cx=\"46\" cy=\"6\" r=\"6\" fill=\"#27C93F\" stroke=\"#1AAB29\" stroke-width=\".5\"><\/circle><\/g><\/svg><\/span><span role=\"button\" tabindex=\"0\" style=\"color:#d8dee9ff;display:none\" aria-label=\"Copy\" class=\"code-block-pro-copy-button\"><pre class=\"code-block-pro-copy-button-pre\" aria-hidden=\"true\"><textarea class=\"code-block-pro-copy-button-textarea\" tabindex=\"-1\" aria-hidden=\"true\" readonly>s1 = \"hello\"\ns2 = \"hello \"\nprint(s1 == s2)  # False\n<\/textarea><\/pre><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" style=\"width:24px;height:24px\" fill=\"none\" viewBox=\"0 0 24 24\" stroke=\"currentColor\" stroke-width=\"2\"><path class=\"with-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4\"><\/path><path class=\"without-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2\"><\/path><\/svg><\/span><pre class=\"shiki nord\" style=\"background-color: #2e3440ff\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color: #D8DEE9FF\">s1 <\/span><span style=\"color: #81A1C1\">=<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #ECEFF4\">&quot;<\/span><span style=\"color: #A3BE8C\">hello<\/span><span style=\"color: #ECEFF4\">&quot;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D8DEE9FF\">s2 <\/span><span style=\"color: #81A1C1\">=<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #ECEFF4\">&quot;<\/span><span style=\"color: #A3BE8C\">hello <\/span><span style=\"color: #ECEFF4\">&quot;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #88C0D0\">print<\/span><span style=\"color: #ECEFF4\">(<\/span><span style=\"color: #D8DEE9FF\">s1 <\/span><span style=\"color: #81A1C1\">==<\/span><span style=\"color: #D8DEE9FF\"> s2<\/span><span style=\"color: #ECEFF4\">)<\/span><span style=\"color: #D8DEE9FF\">  <\/span><span style=\"color: #616E88\"># False<\/span><\/span>\n<span class=\"line\"><\/span><\/code><\/pre><\/div>\n\n\n\n<p>\u0110\u1ec3 tr\u00e1nh l\u1ed7i n\u00e0y, khi x\u1eed l\u00fd d\u1eef li\u1ec7u nh\u1eadp t\u1eeb ng\u01b0\u1eddi d\u00f9ng ho\u1eb7c t\u1eeb file, n\u00ean d\u00f9ng <code>.strip()<\/code> \u0111\u1ec3 lo\u1ea1i b\u1ecf kho\u1ea3ng tr\u1eafng th\u1eeba.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">3.4 Qu\u00ean t\u00ednh b\u1ea5t bi\u1ebfn c\u1ee7a string<\/h3>\n\n\n\n<p>V\u00ec string l\u00e0 <strong>immutable<\/strong>, b\u1ea1n kh\u00f4ng th\u1ec3 thay \u0111\u1ed5i tr\u1ef1c ti\u1ebfp m\u1ed9t k\u00fd t\u1ef1 trong chu\u1ed7i. C\u00e2u l\u1ec7nh sau s\u1ebd g\u00e2y l\u1ed7i:<\/p>\n\n\n\n<div class=\"wp-block-kevinbatdorf-code-block-pro\" data-code-block-pro-font-family=\"Code-Pro-JetBrains-Mono\" style=\"font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)\"><span style=\"display:block;padding:16px 0 0 16px;margin-bottom:-1px;width:100%;text-align:left;background-color:#2e3440ff\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"54\" height=\"14\" viewBox=\"0 0 54 14\"><g fill=\"none\" fill-rule=\"evenodd\" transform=\"translate(1 1)\"><circle cx=\"6\" cy=\"6\" r=\"6\" fill=\"#FF5F56\" stroke=\"#E0443E\" stroke-width=\".5\"><\/circle><circle cx=\"26\" cy=\"6\" r=\"6\" fill=\"#FFBD2E\" stroke=\"#DEA123\" stroke-width=\".5\"><\/circle><circle cx=\"46\" cy=\"6\" r=\"6\" fill=\"#27C93F\" stroke=\"#1AAB29\" stroke-width=\".5\"><\/circle><\/g><\/svg><\/span><span role=\"button\" tabindex=\"0\" style=\"color:#d8dee9ff;display:none\" aria-label=\"Copy\" class=\"code-block-pro-copy-button\"><pre class=\"code-block-pro-copy-button-pre\" aria-hidden=\"true\"><textarea class=\"code-block-pro-copy-button-textarea\" tabindex=\"-1\" aria-hidden=\"true\" readonly>s = \"hello\"\ns&#91;0&#93; = \"H\"  # TypeError<\/textarea><\/pre><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" style=\"width:24px;height:24px\" fill=\"none\" viewBox=\"0 0 24 24\" stroke=\"currentColor\" stroke-width=\"2\"><path class=\"with-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4\"><\/path><path class=\"without-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2\"><\/path><\/svg><\/span><pre class=\"shiki nord\" style=\"background-color: #2e3440ff\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color: #D8DEE9FF\">s <\/span><span style=\"color: #81A1C1\">=<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #ECEFF4\">&quot;<\/span><span style=\"color: #A3BE8C\">hello<\/span><span style=\"color: #ECEFF4\">&quot;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D8DEE9FF\">s<\/span><span style=\"color: #ECEFF4\">&#91;<\/span><span style=\"color: #B48EAD\">0<\/span><span style=\"color: #ECEFF4\">&#93;<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #81A1C1\">=<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #ECEFF4\">&quot;<\/span><span style=\"color: #A3BE8C\">H<\/span><span style=\"color: #ECEFF4\">&quot;<\/span><span style=\"color: #D8DEE9FF\">  <\/span><span style=\"color: #616E88\"># TypeError<\/span><\/span><\/code><\/pre><\/div>\n\n\n\n<p>C\u00e1ch \u0111\u00fang l\u00e0 t\u1ea1o m\u1ed9t chu\u1ed7i m\u1edbi:<\/p>\n\n\n\n<div class=\"wp-block-kevinbatdorf-code-block-pro\" data-code-block-pro-font-family=\"Code-Pro-JetBrains-Mono\" style=\"font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)\"><span style=\"display:block;padding:16px 0 0 16px;margin-bottom:-1px;width:100%;text-align:left;background-color:#2e3440ff\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"54\" height=\"14\" viewBox=\"0 0 54 14\"><g fill=\"none\" fill-rule=\"evenodd\" transform=\"translate(1 1)\"><circle cx=\"6\" cy=\"6\" r=\"6\" fill=\"#FF5F56\" stroke=\"#E0443E\" stroke-width=\".5\"><\/circle><circle cx=\"26\" cy=\"6\" r=\"6\" fill=\"#FFBD2E\" stroke=\"#DEA123\" stroke-width=\".5\"><\/circle><circle cx=\"46\" cy=\"6\" r=\"6\" fill=\"#27C93F\" stroke=\"#1AAB29\" stroke-width=\".5\"><\/circle><\/g><\/svg><\/span><span role=\"button\" tabindex=\"0\" style=\"color:#d8dee9ff;display:none\" aria-label=\"Copy\" class=\"code-block-pro-copy-button\"><pre class=\"code-block-pro-copy-button-pre\" aria-hidden=\"true\"><textarea class=\"code-block-pro-copy-button-textarea\" tabindex=\"-1\" aria-hidden=\"true\" readonly>s = \"H\" + s&#91;1:&#93;<\/textarea><\/pre><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" style=\"width:24px;height:24px\" fill=\"none\" viewBox=\"0 0 24 24\" stroke=\"currentColor\" stroke-width=\"2\"><path class=\"with-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4\"><\/path><path class=\"without-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2\"><\/path><\/svg><\/span><pre class=\"shiki nord\" style=\"background-color: #2e3440ff\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color: #D8DEE9FF\">s <\/span><span style=\"color: #81A1C1\">=<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #ECEFF4\">&quot;<\/span><span style=\"color: #A3BE8C\">H<\/span><span style=\"color: #ECEFF4\">&quot;<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #81A1C1\">+<\/span><span style=\"color: #D8DEE9FF\"> s<\/span><span style=\"color: #ECEFF4\">&#91;<\/span><span style=\"color: #B48EAD\">1<\/span><span style=\"color: #ECEFF4\">:&#93;<\/span><\/span><\/code><\/pre><\/div>\n\n\n\n<h2 class=\"wp-block-heading\">4. \u1ee8ng d\u1ee5ng th\u1ef1c t\u1ebf c\u1ee7a string<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">4.1 In th\u00f4ng tin, nh\u1eadp xu\u1ea5t d\u1eef li\u1ec7u<\/h3>\n\n\n\n<p>String \u0111\u01b0\u1ee3c d\u00f9ng trong h\u1ea7u h\u1ebft c\u00e1c l\u1ec7nh <code>print()<\/code> \u0111\u1ec3 hi\u1ec3n th\u1ecb k\u1ebft qu\u1ea3, v\u00e0 <code>input()<\/code> \u0111\u1ec3 nh\u1eadn d\u1eef li\u1ec7u t\u1eeb ng\u01b0\u1eddi d\u00f9ng. \u0110\u00e2y l\u00e0 nh\u1eefng thao t\u00e1c c\u01a1 b\u1ea3n nh\u1ea5t m\u00e0 b\u1ea5t k\u1ef3 ch\u01b0\u01a1ng tr\u00ecnh Python n\u00e0o c\u0169ng c\u1ea7n.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">4.2 L\u01b0u tr\u1eef v\u00e0 x\u1eed l\u00fd file v\u0103n b\u1ea3n<\/h3>\n\n\n\n<p>Khi \u0111\u1ecdc\/ghi file <code>.txt<\/code>, <code>.csv<\/code>, <code>.json<\/code>, d\u1eef li\u1ec7u b\u1ea1n nh\u1eadn v\u1ec1 th\u01b0\u1eddng \u1edf d\u1ea1ng string. V\u00ed d\u1ee5:<\/p>\n\n\n\n<div class=\"wp-block-kevinbatdorf-code-block-pro\" data-code-block-pro-font-family=\"Code-Pro-JetBrains-Mono\" style=\"font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)\"><span style=\"display:block;padding:16px 0 0 16px;margin-bottom:-1px;width:100%;text-align:left;background-color:#2e3440ff\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"54\" height=\"14\" viewBox=\"0 0 54 14\"><g fill=\"none\" fill-rule=\"evenodd\" transform=\"translate(1 1)\"><circle cx=\"6\" cy=\"6\" r=\"6\" fill=\"#FF5F56\" stroke=\"#E0443E\" stroke-width=\".5\"><\/circle><circle cx=\"26\" cy=\"6\" r=\"6\" fill=\"#FFBD2E\" stroke=\"#DEA123\" stroke-width=\".5\"><\/circle><circle cx=\"46\" cy=\"6\" r=\"6\" fill=\"#27C93F\" stroke=\"#1AAB29\" stroke-width=\".5\"><\/circle><\/g><\/svg><\/span><span role=\"button\" tabindex=\"0\" style=\"color:#d8dee9ff;display:none\" aria-label=\"Copy\" class=\"code-block-pro-copy-button\"><pre class=\"code-block-pro-copy-button-pre\" aria-hidden=\"true\"><textarea class=\"code-block-pro-copy-button-textarea\" tabindex=\"-1\" aria-hidden=\"true\" readonly>with open(\"log.txt\", \"r\", encoding=\"utf-8\") as f:\n    content = f.read()<\/textarea><\/pre><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" style=\"width:24px;height:24px\" fill=\"none\" viewBox=\"0 0 24 24\" stroke=\"currentColor\" stroke-width=\"2\"><path class=\"with-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4\"><\/path><path class=\"without-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2\"><\/path><\/svg><\/span><pre class=\"shiki nord\" style=\"background-color: #2e3440ff\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color: #81A1C1\">with<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #88C0D0\">open<\/span><span style=\"color: #ECEFF4\">(<\/span><span style=\"color: #ECEFF4\">&quot;<\/span><span style=\"color: #A3BE8C\">log.txt<\/span><span style=\"color: #ECEFF4\">&quot;<\/span><span style=\"color: #ECEFF4\">,<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #ECEFF4\">&quot;<\/span><span style=\"color: #A3BE8C\">r<\/span><span style=\"color: #ECEFF4\">&quot;<\/span><span style=\"color: #ECEFF4\">,<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #D8DEE9\">encoding<\/span><span style=\"color: #81A1C1\">=<\/span><span style=\"color: #ECEFF4\">&quot;<\/span><span style=\"color: #A3BE8C\">utf-8<\/span><span style=\"color: #ECEFF4\">&quot;<\/span><span style=\"color: #ECEFF4\">)<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #81A1C1\">as<\/span><span style=\"color: #D8DEE9FF\"> f<\/span><span style=\"color: #ECEFF4\">:<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D8DEE9FF\">    content <\/span><span style=\"color: #81A1C1\">=<\/span><span style=\"color: #D8DEE9FF\"> f<\/span><span style=\"color: #ECEFF4\">.<\/span><span style=\"color: #88C0D0\">read<\/span><span style=\"color: #ECEFF4\">()<\/span><\/span><\/code><\/pre><\/div>\n\n\n\n<p>\u1ede m\u1ee9c c\u01a1 b\u1ea3n, l\u00e0m vi\u1ec7c v\u1edbi file g\u1ea7n nh\u01b0 \u0111\u1ed3ng ngh\u0129a v\u1edbi l\u00e0m vi\u1ec7c v\u1edbi string.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">4.3 L\u00e0m vi\u1ec7c v\u1edbi d\u1eef li\u1ec7u JSON, HTML, XML<\/h3>\n\n\n\n<p>C\u00e1c \u0111\u1ecbnh d\u1ea1ng d\u1eef li\u1ec7u ph\u1ed5 bi\u1ebfn nh\u01b0 JSON, HTML hay XML th\u1ef1c ch\u1ea5t \u0111\u1ec1u l\u00e0 chu\u1ed7i v\u0103n b\u1ea3n. Khi l\u1ea5y d\u1eef li\u1ec7u t\u1eeb API, b\u1ea1n s\u1ebd nh\u1eadn \u0111\u01b0\u1ee3c string JSON r\u1ed3i parse th\u00e0nh object \u0111\u1ec3 x\u1eed l\u00fd.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">4.4 X\u1eed l\u00fd d\u1eef li\u1ec7u trong AI, NLP<\/h3>\n\n\n\n<p>Trong c\u00e1c \u1ee9ng d\u1ee5ng tr\u00ed tu\u1ec7 nh\u00e2n t\u1ea1o, \u0111\u1eb7c bi\u1ec7t l\u00e0 <strong>Natural Language Processing (X\u1eed l\u00fd ng\u00f4n ng\u1eef t\u1ef1 nhi\u00ean)<\/strong>, string ch\u00ednh l\u00e0 d\u1eef li\u1ec7u \u0111\u1ea7u v\u00e0o. V\u00ed d\u1ee5, khi x\u00e2y d\u1ef1ng chatbot, b\u1ea1n ph\u1ea3i x\u1eed l\u00fd chu\u1ed7i v\u0103n b\u1ea3n ng\u01b0\u1eddi d\u00f9ng nh\u1eadp v\u00e0o, sau \u0111\u00f3 ph\u00e2n t\u00edch, g\u00e1n nh\u00e3n, t\u00e1ch t\u1eeb (tokenization)\u2026<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">4.5 T\u1ea1o log, b\u00e1o c\u00e1o ho\u1eb7c template \u0111\u1ed9ng<\/h3>\n\n\n\n<p>Khi vi\u1ebft \u1ee9ng d\u1ee5ng l\u1edbn, b\u1ea1n s\u1ebd th\u01b0\u1eddng xuy\u00ean c\u1ea7n ghi l\u1ea1i log ho\u1eb7c sinh b\u00e1o c\u00e1o. C\u00e1c thao t\u00e1c n\u00e0y \u0111\u1ec1u xoay quanh string. V\u1edbi Python, b\u1ea1n c\u00f3 th\u1ec3 d\u00f9ng f-string \u0111\u1ec3 \u0111\u1ecbnh d\u1ea1ng \u0111\u1ed9ng:<\/p>\n\n\n\n<div class=\"wp-block-kevinbatdorf-code-block-pro\" data-code-block-pro-font-family=\"Code-Pro-JetBrains-Mono\" style=\"font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)\"><span style=\"display:block;padding:16px 0 0 16px;margin-bottom:-1px;width:100%;text-align:left;background-color:#2e3440ff\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"54\" height=\"14\" viewBox=\"0 0 54 14\"><g fill=\"none\" fill-rule=\"evenodd\" transform=\"translate(1 1)\"><circle cx=\"6\" cy=\"6\" r=\"6\" fill=\"#FF5F56\" stroke=\"#E0443E\" stroke-width=\".5\"><\/circle><circle cx=\"26\" cy=\"6\" r=\"6\" fill=\"#FFBD2E\" stroke=\"#DEA123\" stroke-width=\".5\"><\/circle><circle cx=\"46\" cy=\"6\" r=\"6\" fill=\"#27C93F\" stroke=\"#1AAB29\" stroke-width=\".5\"><\/circle><\/g><\/svg><\/span><span role=\"button\" tabindex=\"0\" style=\"color:#d8dee9ff;display:none\" aria-label=\"Copy\" class=\"code-block-pro-copy-button\"><pre class=\"code-block-pro-copy-button-pre\" aria-hidden=\"true\"><textarea class=\"code-block-pro-copy-button-textarea\" tabindex=\"-1\" aria-hidden=\"true\" readonly>name = \"KienThucMo\"\nage = 25\nprint(f\"User {name} is {age} years old.\")<\/textarea><\/pre><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" style=\"width:24px;height:24px\" fill=\"none\" viewBox=\"0 0 24 24\" stroke=\"currentColor\" stroke-width=\"2\"><path class=\"with-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4\"><\/path><path class=\"without-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2\"><\/path><\/svg><\/span><pre class=\"shiki nord\" style=\"background-color: #2e3440ff\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color: #D8DEE9FF\">name <\/span><span style=\"color: #81A1C1\">=<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #ECEFF4\">&quot;<\/span><span style=\"color: #A3BE8C\">KienThucMo<\/span><span style=\"color: #ECEFF4\">&quot;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D8DEE9FF\">age <\/span><span style=\"color: #81A1C1\">=<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #B48EAD\">25<\/span><\/span>\n<span class=\"line\"><span style=\"color: #88C0D0\">print<\/span><span style=\"color: #ECEFF4\">(<\/span><span style=\"color: #81A1C1\">f<\/span><span style=\"color: #A3BE8C\">&quot;User <\/span><span style=\"color: #EBCB8B\">{<\/span><span style=\"color: #D8DEE9FF\">name<\/span><span style=\"color: #EBCB8B\">}<\/span><span style=\"color: #A3BE8C\"> is <\/span><span style=\"color: #EBCB8B\">{<\/span><span style=\"color: #D8DEE9FF\">age<\/span><span style=\"color: #EBCB8B\">}<\/span><span style=\"color: #A3BE8C\"> years old.&quot;<\/span><span style=\"color: #ECEFF4\">)<\/span><\/span><\/code><\/pre><\/div>\n\n\n\n<h2 class=\"wp-block-heading\">5. K\u1ebft lu\u1eadn<\/h2>\n\n\n\n<p>String trong Python kh\u00f4ng ch\u1ec9 l\u00e0 \u201cm\u1ed9t \u0111o\u1ea1n ch\u1eef\u201d, m\u00e0 l\u00e0 m\u1ed9t ki\u1ec3u d\u1eef li\u1ec7u quan tr\u1ecdng v\u1edbi nhi\u1ec1u \u0111\u1eb7c t\u00ednh c\u1ea7n ghi nh\u1edb: l\u00e0 <strong>chu\u1ed7i k\u00fd t\u1ef1 Unicode<\/strong>, <strong>immutable<\/strong>, v\u00e0 <strong>kh\u00e1c bi\u1ec7t v\u1edbi bytes<\/strong>. Hi\u1ec3u r\u00f5 nh\u1eefng kh\u00e1i ni\u1ec7m n\u00e0y s\u1ebd gi\u00fap b\u1ea1n l\u00e0m vi\u1ec7c v\u1edbi Python m\u1ed9t c\u00e1ch t\u1ef1 tin h\u01a1n v\u00e0 h\u1ea1n ch\u1ebf nh\u1eefng l\u1ed7i th\u01b0\u1eddng g\u1eb7p khi x\u1eed l\u00fd v\u0103n b\u1ea3n.<\/p>\n\n\n\n<p>\u1ede c\u00e1c b\u00e0i vi\u1ebft ti\u1ebfp theo, b\u1ea1n c\u00f3 th\u1ec3 t\u00ecm hi\u1ec3u s\u00e2u h\u01a1n v\u1ec1 <strong>c\u00e1c thao t\u00e1c v\u1edbi string<\/strong> nh\u01b0 slicing, n\u1ed1i chu\u1ed7i, c\u00e1c ph\u01b0\u01a1ng th\u1ee9c (<code>split<\/code>, <code>join<\/code>, <code>replace<\/code>&#8230;), c\u0169ng nh\u01b0 c\u00e1c k\u1ef9 thu\u1eadt n\u00e2ng cao nh\u01b0 regex hay Unicode normalization.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">6. T\u00e0i li\u1ec7u tham kh\u1ea3o<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Python Software Foundation. (2024). <em>Text Sequence Type \u2014 str<\/em>. In Python documentation. Retrieved from <a>https:\/\/docs.python.org\/3\/library\/stdtypes.html#text-sequence-type-str<\/a><\/li>\n\n\n\n<li>Real Python. (n.d.). <em>Python Strings<\/em>. Real Python. Retrieved from <a>https:\/\/realpython.com\/python-strings\/<\/a><\/li>\n\n\n\n<li>W3Schools. (n.d.). <em>Python Strings<\/em>. W3Schools. Retrieved from <a>https:\/\/www.w3schools.com\/python\/python_strings.asp<\/a><\/li>\n\n\n\n<li>Python for Professionals: Learning Python as a Second Language: <a href=\"https:\/\/click.linksynergy.com\/link?id=*C\/UgjGtUZ8&amp;offerid=1562891.3721710002222624882405978&amp;type=15&amp;murl=https%3A%2F%2Fwww.kobo.com%2Fus%2Fen%2Febook%2Fpython-for-professionals-3\" target=\"_blank\" rel=\"noopener\">https:\/\/www.kobo.com\/us\/en\/ebook\/python-for-professionals-3<\/a><\/li>\n\n\n\n<li>Python: Deeper Insights into Machine Learning: <a href=\"https:\/\/click.linksynergy.com\/link?id=*C\/UgjGtUZ8&amp;offerid=1562891.3721710015810095319857183&amp;type=15&amp;murl=https%3A%2F%2Fwww.kobo.com%2Fus%2Fen%2Febook%2Fpython-deeper-insights-into-machine-learning\" target=\"_blank\" rel=\"noopener\">https:\/\/www.kobo.com\/us\/en\/ebook\/python-deeper-insights-into-machine-learning<\/a><\/li>\n\n\n\n<li>DataFusion Python Bindings in Practice: The Complete Guide for Developers and Engineers: <a href=\"https:\/\/click.linksynergy.com\/link?id=*C\/UgjGtUZ8&amp;offerid=1562891.3721710049093362364820452&amp;type=15&amp;murl=https%3A%2F%2Fwww.kobo.com%2Fus%2Fen%2Febook%2Fdatafusion-python-bindings-in-practice\" target=\"_blank\" rel=\"noopener\">https:\/\/www.kobo.com\/us\/en\/ebook\/datafusion-python-bindings-in-practice<\/a><\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>String trong Python kh\u00f4ng ch\u1ec9 l\u00e0 \u201cm\u1ed9t \u0111o\u1ea1n ch\u1eef\u201d, m\u00e0 l\u00e0 m\u1ed9t ki\u1ec3u d\u1eef li\u1ec7u quan tr\u1ecdng v\u1edbi nhi\u1ec1u \u0111\u1eb7c t\u00ednh c\u1ea7n ghi nh\u1edb: l\u00e0 chu\u1ed7i k\u00fd t\u1ef1 Unicode, immutable, v\u00e0 kh\u00e1c bi\u1ec7t v\u1edbi bytes. Hi\u1ec3u r\u00f5 nh\u1eefng kh\u00e1i ni\u1ec7m n\u00e0y s\u1ebd gi\u00fap b\u1ea1n l\u00e0m vi\u1ec7c v\u1edbi Python m\u1ed9t c\u00e1ch t\u1ef1 tin h\u01a1n v\u00e0 h\u1ea1n ch\u1ebf nh\u1eefng l\u1ed7i th\u01b0\u1eddng g\u1eb7p khi x\u1eed l\u00fd v\u0103n b\u1ea3n.<\/p>\n","protected":false},"author":1,"featured_media":1624,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"googlesitekit_rrm_CAowieHDDA:productID":"","footnotes":""},"categories":[41],"tags":[40],"class_list":["post-1474","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-kien-thuc-lap-trinh","tag-python-co-ban"],"_links":{"self":[{"href":"https:\/\/kienthucmo.com\/vi\/wp-json\/wp\/v2\/posts\/1474","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/kienthucmo.com\/vi\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/kienthucmo.com\/vi\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/kienthucmo.com\/vi\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/kienthucmo.com\/vi\/wp-json\/wp\/v2\/comments?post=1474"}],"version-history":[{"count":3,"href":"https:\/\/kienthucmo.com\/vi\/wp-json\/wp\/v2\/posts\/1474\/revisions"}],"predecessor-version":[{"id":2433,"href":"https:\/\/kienthucmo.com\/vi\/wp-json\/wp\/v2\/posts\/1474\/revisions\/2433"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/kienthucmo.com\/vi\/wp-json\/wp\/v2\/media\/1624"}],"wp:attachment":[{"href":"https:\/\/kienthucmo.com\/vi\/wp-json\/wp\/v2\/media?parent=1474"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/kienthucmo.com\/vi\/wp-json\/wp\/v2\/categories?post=1474"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/kienthucmo.com\/vi\/wp-json\/wp\/v2\/tags?post=1474"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}