<!DOCTYPE html>
    <html lang="vi" xmlns="http://www.w3.org/1999/xhtml" prefix="og: http://ogp.me/ns#">
    <head>
<title>Hàm print&#40;&#41; trong Python</title>
<meta name="description" content="Hàm print&#40;&#41; trong Python - Savefile - Tin Tức - https&#x3A;&#x002F;&#x002F;www.nguoicodonvn2008.info&#x002F;vi&#x002F;news&#x002F;savefile&#x002F;kien-thuc-may-tinh&#x002F;ham-print-trong-python-11563.html">
<meta name="author" content=".: Nguoicodonvn2008.info - Cõi lòng người cô đơn :.">
<meta name="copyright" content=".: Nguoicodonvn2008.info - Cõi lòng người cô đơn :. [admin@nguoicodonvn2008.info]">
<meta name="robots" content="index, archive, follow, noodp">
<meta name="googlebot" content="index,archive,follow,noodp">
<meta name="msnbot" content="all,index,follow">
<meta name="generator" content="NukeViet v4.5">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<meta property="og:title" content="Hàm print&#40;&#41; trong Python">
<meta property="og:type" content="website">
<meta property="og:description" content="Savefile - Tin Tức - https&#x3A;&#x002F;&#x002F;www.nguoicodonvn2008.info&#x002F;vi&#x002F;news&#x002F;savefile&#x002F;kien-thuc-may-tinh&#x002F;ham-print-trong-python-11563.html">
<meta property="og:site_name" content=".&#x3A; Nguoicodonvn2008.info - Cõi lòng người cô đơn &#x3A;.">
<meta property="og:url" content="https://www.nguoicodonvn2008.info/vi/news/savefile/kien-thuc-may-tinh/ham-print-trong-python-11563.html">
<link rel="shortcut icon" href="https://www.nguoicodonvn2008.info/favicon.ico">
<link rel="canonical" href="https://www.nguoicodonvn2008.info/vi/news/savefile/kien-thuc-may-tinh/ham-print-trong-python-11563.html">
<link rel="alternate" href="https://www.nguoicodonvn2008.info/vi/news/rss/" title="Tin Tức" type="application/rss+xml">
<link rel="alternate" href="https://www.nguoicodonvn2008.info/vi/news/rss/karaoke-dual/" title="Tin Tức - Karaoke Dual" type="application/rss+xml">
<link rel="alternate" href="https://www.nguoicodonvn2008.info/vi/news/rss/nhac-tre/" title="Tin Tức - Nhạc trẻ" type="application/rss+xml">
<link rel="alternate" href="https://www.nguoicodonvn2008.info/vi/news/rss/tru-tinh/" title="Tin Tức - Trữ tình" type="application/rss+xml">
<link rel="alternate" href="https://www.nguoicodonvn2008.info/vi/news/rss/nuoc-ngoai/" title="Tin Tức - Nước ngoài" type="application/rss+xml">
<link rel="alternate" href="https://www.nguoicodonvn2008.info/vi/news/rss/remix/" title="Tin Tức - Remix" type="application/rss+xml">
<link rel="alternate" href="https://www.nguoicodonvn2008.info/vi/news/rss/tam-su-tinh-yeu/" title="Tin Tức - Tâm sự tình yêu" type="application/rss+xml">
<link rel="alternate" href="https://www.nguoicodonvn2008.info/vi/news/rss/tho-suu-tam/" title="Tin Tức - Thơ sưu tầm" type="application/rss+xml">
<link rel="alternate" href="https://www.nguoicodonvn2008.info/vi/news/rss/cuoc-song/" title="Tin Tức - Cuộc sống" type="application/rss+xml">
<link rel="alternate" href="https://www.nguoicodonvn2008.info/vi/news/rss/phan-mem/" title="Tin Tức - Phần mềm" type="application/rss+xml">
<link rel="alternate" href="https://www.nguoicodonvn2008.info/vi/news/rss/kien-thuc-may-tinh/" title="Tin Tức - Kiến thức máy tính" type="application/rss+xml">
<link rel="alternate" href="https://www.nguoicodonvn2008.info/vi/news/rss/hoc-tap/" title="Tin Tức - Học tập" type="application/rss+xml">
<link rel="alternate" href="https://www.nguoicodonvn2008.info/vi/news/rss/tai-lieu/" title="Tin Tức - Tài liệu" type="application/rss+xml">
<link rel="alternate" href="https://www.nguoicodonvn2008.info/vi/news/rss/de-thi/" title="Tin Tức - Đề thi" type="application/rss+xml">
<link rel="preload" as="style" href="https://www.nguoicodonvn2008.info/assets/css/font-awesome.min.css" type="text/css">
<link rel="preload" as="style" href="https://www.nguoicodonvn2008.info/themes/default/css/bootstrap.non-responsive.css" type="text/css">
<link rel="preload" as="style" href="https://www.nguoicodonvn2008.info/themes/default/css/style.css" type="text/css">
<link rel="preload" as="style" href="https://www.nguoicodonvn2008.info/themes/default/css/style.non-responsive.css" type="text/css">
<link rel="preload" as="style" href="https://www.nguoicodonvn2008.info/themes/default/css/news.css" type="text/css">
<link rel="preload" as="style" href="https://www.nguoicodonvn2008.info/themes/default/css/custom.css" type="text/css">
<link rel="preload" as="script" href="https://www.nguoicodonvn2008.info/assets/js/jquery/jquery.min.js" type="text/javascript">
<link rel="preload" as="script" href="https://www.nguoicodonvn2008.info/assets/js/language/vi.js" type="text/javascript">
<link rel="preload" as="script" href="https://www.nguoicodonvn2008.info/assets/js/DOMPurify/purify3.js" type="text/javascript">
<link rel="preload" as="script" href="https://www.nguoicodonvn2008.info/assets/js/global.js" type="text/javascript">
<link rel="preload" as="script" href="https://www.nguoicodonvn2008.info/assets/js/site.js" type="text/javascript">
<link rel="preload" as="script" href="https://www.nguoicodonvn2008.info/themes/default/js/news.js" type="text/javascript">
<link rel="preload" as="script" href="https://www.nguoicodonvn2008.info/themes/default/js/main.js" type="text/javascript">
<link rel="preload" as="script" href="https://www.nguoicodonvn2008.info/themes/default/js/custom.js" type="text/javascript">
<link rel="preload" as="script" href="https://www.nguoicodonvn2008.info/themes/default/js/bootstrap.min.js" type="text/javascript">
<link rel="stylesheet" href="https://www.nguoicodonvn2008.info/assets/css/font-awesome.min.css">
<link rel="stylesheet" href="https://www.nguoicodonvn2008.info/themes/default/css/bootstrap.non-responsive.css">
<link rel="stylesheet" href="https://www.nguoicodonvn2008.info/themes/default/css/style.css">
<link rel="stylesheet" href="https://www.nguoicodonvn2008.info/themes/default/css/style.non-responsive.css">
<link rel="StyleSheet" href="https://www.nguoicodonvn2008.info/themes/default/css/news.css">
<link rel="stylesheet" href="https://www.nguoicodonvn2008.info/themes/default/css/custom.css">
<style type="text/css">
	body{background: #fff;}
</style>
    </head>
    <body>
<div id="print">
	<div id="hd_print">
		<h2 class="pull-left">.&#x3A; Nguoicodonvn2008.info - Cõi lòng người cô đơn &#x3A;.</h2>
		<p class="pull-right"><a title=".&#x3A; Nguoicodonvn2008.info - Cõi lòng người cô đơn &#x3A;." href="https://www.nguoicodonvn2008.info/">https://www.nguoicodonvn2008.info</a></p>
	</div>
	<div class="clear"></div>
	<hr />
	<div id="content">
		<h1>Hàm print&#40;&#41; trong Python</h1>
		<ul class="list-inline">
			<li>Chủ nhật - 09/11/2025 22:10</li>
			<li class="hidden-print txtrequired"><em class="fa fa-print">&nbsp;</em><a title="In ra" href="javascript:;" onclick="window.print()">In ra</a></li>
			<li class="hidden-print txtrequired"><em class="fa fa-power-off">&nbsp;</em><a title="Đóng cửa sổ này" href="javascript:;" onclick="window.close()">Đóng cửa sổ này</a></li>
		</ul>
		<div class="clear"></div>
		<div id="hometext">
		</div>
				<div class="imghome">
			<img alt="Hàm print&#40;&#41; trong Python" src="https://st.quantrimang.com/photos/image/2018/10/16/mang-trong-Python-200-size-80x80-znd.jpg" width="460" class="img-thumbnail" />
		</div>
		<div class="clear"></div>
		<div id="bodytext" class="clearfix">
			<p style="text-align: justify;"><strong>Print trong Python là gì? Cách dùng lệnh print trong Python như thế nào?&nbsp;</strong>Hãy cùng nhau tìm hiểu nhé.</p>

<div style="text-align: justify;">&nbsp;</div>

<p style="text-align: justify;">Python là một trong số ngôn ngữ lập trình được ưa chuộng nhất hiện nay. Khi ngành lập trình đang ngày càng hot, ngày càng có nhiều người lựa chọn học ngôn ngữ này. Sở dĩ như vậy bởi nó có tính ứng dụng cao và linh hoạt trong cách sử dụng.</p>

<p style="text-align: justify;">Học Python không khó. Bạn dễ dàng tìm thấy một khóa đào tạo chuyên nghiệp ở một trung tâm hay trường đại học nào đó. Tuy nhiên, nếu muốn tự học, bạn có thể tham khảo các bài hướng trên sử dụng Python trên Quantrimang.com</p>

<p style="text-align: justify;">Nếu cũng giống như hầu hết người dùng Python, thì có lẽ bạn đã bắt đầu hành trình Python của mình bằng việc tìm hiểu về print(). Nó đã giúp bạn viết nên dòng lệnh &quot;Hello, World!&quot; của riêng mình và thổi hồn vào mã nguồn. Hơn thế nữa, bạn có thể sử dụng nó để định dạng tin nhắn và thậm chí tìm ra một số lỗi. Nhưng nếu bạn nghĩ rằng đó là tất cả những gì cần biết về hàm print() của Python, thì bạn đang bỏ lỡ rất nhiều điều!</p>

<p style="text-align: justify;">Hãy đọc tiếp để tận dụng tối đa hàm nhỏ bé tưởng chừng nhàm chán và ít được chú ý này. Hướng dẫn này sẽ giúp bạn nhanh chóng làm quen với cách sử dụng print() hiệu quả trong Python. Tuy nhiên, hãy chuẩn bị tinh thần cho việc tìm hiểu sâu hơn khi bạn xem qua các phần. Bạn có thể sẽ ngạc nhiên bởi những gì print() mang lại!</p>

<p style="text-align: justify;"><strong>Hàm print()</strong>&nbsp;trong&nbsp;Python&nbsp;có tác dụng hiển thị dữ diệu ra màn hình khi chương trình thực thi.</p>

<p style="text-align: justify;"><strong>Cú pháp đầy đủ của print():</strong></p>

<pre id="pre0" style="text-align: justify;">
print(*objects, sep=&#039; &#039;, end=&#039;\n&#039;, file=sys.stdout, flush=False)</pre>

<p style="text-align: justify;"><strong>Tham số của hàm print():</strong></p>

<ul>
	<li style="text-align: justify;"><code>objects</code>: đối tượng được in, có thể có nhiều đối tượng. Sẽ được chuyển đổi thành chuỗi trước khi hiển thị ra màn hình.</li>
	<li style="text-align: justify;"><code>sep</code>: cách tách riêng các đối tượng, giá trị mặc định là một khoảng trắng&nbsp;.</li>
	<li style="text-align: justify;"><code>end</code>: giá trị cuối cùng được in ra màn hình.</li>
	<li style="text-align: justify;"><code>file</code>: mặc định hàm print sẽ ghi nội dung vào file&nbsp;<em>sys.stdout.</em></li>
	<li style="text-align: justify;"><code>flush</code>: giá trị mặc định giá trị là False.</li>
</ul>

<h2 style="text-align: justify;">Cách thức hoạt động của hàm print() trong Python?</h2>

<p style="text-align: justify;">Bạn có thể chuyển các biến, chuỗi, số hoặc kiểu dữ liệu khác làm một hoặc nhiều tham số hơn khi dùng hàm print() trong Python. Sau đó, những tham số này hiển thị dưới dạng chuỗi theo hàm str() tương ứng. Để tạo một chuỗi kết quả, các chuỗi đã chuyển đổi được nối với nhau bằng khoảng cách giữa chúng.</p>

<p style="text-align: justify;">Code ví dụ này chuyển hai tham số tên và tuổi sang hàm print.</p>

<pre id="pre1" style="text-align: justify;">
name = &quot;Alice&quot;
age = 25
print(&quot;Hello, my name is&quot;, name, &quot;and I am&quot;, age, &quot;years old.&quot;)</pre>

<p style="text-align: justify;"><strong>Kết quả:</strong></p>

<pre id="pre2" style="text-align: justify;">
Hello, my name is Alice and I am 25 years old.</pre>

<p style="text-align: justify;"><strong>Lưu ý</strong>: sep, end, file và flush đều là các tham số keyword. Nếu bạn muốn sử dụng tham số sep, bạn phải dùng như này:</p>

<pre id="pre3" style="text-align: justify;">
print(*objects, sep = &#039;separator&#039;)</pre>

<p style="text-align: justify;">không được sử dụng:</p>

<pre id="pre4" style="text-align: justify;">
print(*objects, &#039;separator&#039;)</pre>

<p style="text-align: justify;"><strong>Ví dụ 1: Cách print() hoạt động trong Python</strong></p>

<pre id="pre5" style="text-align: justify;">
print(&quot;Học Python rất thú vị.&quot;)a = 5# 2 objectprint(&quot;a =&quot;, a)b = a# 3 objectprint(&#039;a =&#039;, a, &#039;= b&#039;)</pre>

<p style="text-align: justify;">Chạy chương trình, kết quả trả về là:</p>

<pre id="pre6" style="text-align: justify;">
Học Python rất thú vị.a = 5a = 5 = b</pre>

<p style="text-align: justify;">Trong 3 câu lệnh ở ví dụ trên, chỉ có duy nhất tham số object được sử dụng trong các câu lệnh.</p>

<p style="text-align: justify;"><strong>Ví dụ 2: print () với các tham số&nbsp;<em>separator</em>&nbsp;và&nbsp;<em>end</em></strong></p>

<pre id="pre7" style="text-align: justify;">
a = 5print(&quot;a =&quot;, a, sep=&#039;00000&#039;, end=&#039;\n\n\n&#039;)print(&quot;a =&quot;, a, sep=&#039;0&#039;, end=&#039;&#039;)</pre>

<p style="text-align: justify;">Chạy chương trình, kết quả trả về là:</p>

<pre id="pre8" style="text-align: justify;">
a =000005a =05</pre>

<p style="text-align: justify;"><strong>Ví dụ 3: print() với tham số file</strong></p>

<p style="text-align: justify;">Trong Python, bạn có thể print objects với file bằng cách xác định tham số file.</p>

<pre id="pre9" style="text-align: justify;">
sourceFile = open(&#039;python.txt&#039;, &#039;w&#039;)
print(&#039;Pretty cool, huh!&#039;, file = sourceFile)
sourceFile.close()</pre>

<p style="text-align: justify;">Chương trình này cố gắng mở python.txt trong chế độ viết. Nếu file này không tồn tại, tệp&nbsp;<strong>python.txt</strong>&nbsp;được tạo và mở ở chế độ ghi.</p>

<p style="text-align: justify;">Tại đây, ví dụ đã chuyển file<strong>&nbsp;sourceFile</strong>&nbsp;sang tham số file. Đối tượng chuỗi (string)&nbsp;<strong>“Pretty cool, huh!”</strong>&nbsp;được in sang file python.txt (kiểm tra nó trong hệ thống).</p>

<p style="text-align: justify;">Cuối cùng, file này được đóng bằng phương thức close().</p>

<h3 style="text-align: justify;">Cách hướng đầu ra print() vào một file trong Python</h3>

<p style="text-align: justify;">Hầu hết mọi người đều muốn in kết quả sang định dạng tiêu chuẩn hoặc chuẩn dòng lệnh. Tuy nhiên, khi muốn chuyển kết quả trực tiếp tới file hiện có thì phải làm thế nào?</p>

<p style="text-align: justify;">Giả sử bạn có một file text và muốn thêm một chút văn bản bằng hàm print(). Để mở &amp; viết file trong Python, bạn gọi hàm<strong>&nbsp;open()</strong>. Bên trong nó, bạn bao gồm tên file,<strong>&nbsp;output.txt&nbsp;</strong>ở trường hợp này, và thêm chế độ&nbsp;<strong>-w</strong>, nghĩa là chỉ viết.</p>

<p style="text-align: justify;">Ở chế độ này, mỗi lần bạn chạy code nội dung của file sẽ được xóa và thay bằng bất kỳ text mới bạn thêm vào. Nếu không muốn mất nội dung, thay vào đó, bạn có thể dùng chế độ -a, để bổ sung text vào phần cuối của file.</p>

<p style="text-align: justify;">Bên trong hàm&nbsp;<strong>print()&nbsp;</strong>bạn có thể thêm bất kỳ nội dung mong muốn vào file và đặt tham số file bằng tên placeholder đã tạo cho file muốn thêm vào text, ở ví dụ này là&nbsp;<strong>f</strong>.</p>

<pre id="pre10" style="text-align: justify;">
with open(&#039;output.txt&#039;, &#039;w&#039;) as f:
print(&#039;Hello World!&#039;, file=f)</pre>

<p style="text-align: justify;">Hướng dẫn này là một điểm khởi đầu tốt cho những người mới bắt đầu mong muốn thành thạo Python. Bạn có thể muốn thử nghiệm thêm với hàm Print và khám phá thêm một vài chức năng của nó mà có thể đã bị bỏ sót trong hướng dẫn này.</p>

<p style="text-align: justify;">Hi vọng qua bài viết này, bạn đã biết&nbsp;<strong>print trong Python là gì và cách dùng chúng ra sao</strong>. Ngoài ra, bạn có thể tìm hiểu thêm:&nbsp;Các hàm Python tích hợp sẵn</p>

<div style="text-align: justify;">&nbsp;</div>
		</div>
				<div id="author">
						<p>
				<strong>Nguồn tin:</strong>
				Quantrimang.com:
			</p>
		</div>
	</div>
	<div id="footer" class="clearfix">
		<div id="url">
			<strong>URL của bản tin này: </strong><a href="https://www.nguoicodonvn2008.info/vi/news/savefile/kien-thuc-may-tinh/ham-print-trong-python-11563.html" title="Hàm print&#40;&#41; trong Python">https://www.nguoicodonvn2008.info/vi/news/savefile/kien-thuc-may-tinh/ham-print-trong-python-11563.html</a>

		</div>
		<div class="clear"></div>
		<div class="copyright">
			&copy; .&#x3A; Nguoicodonvn2008.info - Cõi lòng người cô đơn &#x3A;.
		</div>
		<div id="contact">
			<a href="mailto:admin@nguoicodonvn2008.info">admin@nguoicodonvn2008.info</a>
		</div>
	</div>
</div>
        <div id="timeoutsess" class="chromeframe">
            Bạn đã không sử dụng Site, <a onclick="timeoutsesscancel();" href="https://www.nguoicodonvn2008.info/#">Bấm vào đây để duy trì trạng thái đăng nhập</a>. Thời gian chờ: <span id="secField"> 60 </span> giây
        </div>
        <div id="openidResult" class="nv-alert" style="display:none"></div>
        <div id="openidBt" data-result="" data-redirect=""></div>
		</script>
		<div class="car-top">
  <span><img src="https://www.nguoicodonvn2008.info/themes/default/images/car.png" alt=""></span>
</div>
<script src="https://www.nguoicodonvn2008.info/assets/js/jquery/jquery.min.js"></script>
<script>var nv_base_siteurl="/",nv_lang_data="vi",nv_lang_interface="vi",nv_name_variable="nv",nv_fc_variable="op",nv_lang_variable="language",nv_module_name="news",nv_func_name="savefile",nv_is_user=0, nv_my_ofs=-4,nv_my_abbr="EDT",nv_cookie_prefix="nv4c_e856T",nv_check_pass_mstime=1738000,nv_area_admin=0,nv_safemode=0,theme_responsive=0,nv_recaptcha_ver=2,nv_recaptcha_sitekey="",nv_recaptcha_type="image",XSSsanitize=1;</script>
<script src="https://www.nguoicodonvn2008.info/assets/js/language/vi.js"></script>
<script src="https://www.nguoicodonvn2008.info/assets/js/DOMPurify/purify3.js"></script>
<script src="https://www.nguoicodonvn2008.info/assets/js/global.js"></script>
<script src="https://www.nguoicodonvn2008.info/assets/js/site.js"></script>
<script src="https://www.nguoicodonvn2008.info/themes/default/js/news.js"></script>
<script src="https://www.nguoicodonvn2008.info/themes/default/js/main.js"></script>
<script src="https://www.nguoicodonvn2008.info/themes/default/js/custom.js"></script>
<script type="application/ld+json">
        {
            "@context": "https://schema.org",
            "@type": "Organization",
            "url": "https://www.nguoicodonvn2008.info",
            "logo": "https://www.nguoicodonvn2008.info/uploads/angel.gif"
        }
        </script>
<script src="https://www.nguoicodonvn2008.info/themes/default/js/bootstrap.min.js"></script>
<script type="text/javascript">
var $scrolltop = $('.car-top');
$scrolltop.on('click', function () {
    $('html,body').animate({
        scrollTop: 0
    }, 800);
    $(this).addClass("car-run");
    setTimeout(function(){ $scrolltop.removeClass('car-run');}, 1000);
    return false;
});
$(window).on('scroll', function ()
{ 
    if($(window).scrollTop() >= 200)
    {
        $scrolltop.addClass("show");
        $scrolltop.addClass("car-down");
    }
    else
    {
       $scrolltop.removeClass("show");
       setTimeout(function(){ $scrolltop.removeClass('car-down');}, 300);
    }
});
</script>
</body>
</html>