Ide Proyek Python: Belajar Bahasa Coding dengan Menyenangkan
Aplikasi untuk GUI​​ Jam alarm menjadi salah satu ide proyek Python sederhana yang banyak digandrungi. Singkatnya, jam alarm adalah program yang memberi tahu ...
print()
Salah satu ide proyek Python terbaik yang dapat dicoba oleh pemula adalah Rolling Dice Simulator. Pada dasarnya, Anda perlu membuat kode Anda dapat memilih dan mencetak nomor acak dalam kisaran angka satu hingga enam. Untuk melakukan hal demikian, Anda harus terbiasa dengan tipe data integer, modul acak, dan fungsi <anchor>print()</anchor>. Jika Anda ingin simulator bertanya apakah pemain ingin melempar dadu kembali, Anda juga akan membutuhkan tindakan while loop.
Guide on the Python print Function: Outputting Messages and Results
Guide on the Python print Function: Outputting Messages and Results

🚨 Time is Running Out: Reserve Your Spot in the Lucky Draw & Claim Rewards! [START NOW](https://www.bitdegree.org/missions)

[![BitDegree Learn](https://www.bitdegree.org/learn/assets/bitdegree-logo-white.svg)](https://www.bitdegree.org/learn/)

Categories

- [Web3](https://www.bitdegree.org)
- [![HTML](https://www.bitdegree.org/learn/storage/media/images/96f8c890-c194-4dd3-a14e-912d5b185652.svg) HTML](https://www.bitdegree.org/learn/html)
- [![CSS](https://www.bitdegree.org/learn/storage/media/images/f58e503f-af9e-4ef7-85a5-1c7a32c74f02.svg) CSS](https://www.bitdegree.org/learn/css)
- [![PHP](https://www.bitdegree.org/learn/storage/media/images/93006417-fb6a-4196-8349-5c66420615cc.svg) PHP](https://www.bitdegree.org/learn/php)
- [![JavaScript](https://www.bitdegree.org/learn/storage/media/images/0179bd53-228d-43e8-9aa9-b89b29bd3a65.svg) JavaScript](https://www.bitdegree.org/learn/javascript)
- [![SQL](https://www.bitdegree.org/learn/storage/media/images/9c5aedcf-73e6-4884-8848-7c8fe9e7f396.svg) SQL](https://www.bitdegree.org/learn/sql)
- [![Bootstrap](https://www.bitdegree.org/learn/storage/media/images/6dc1de53-7104-4c21-a180-bf07d3fa938b.svg) Bootstrap](https://www.bitdegree.org/learn/bootstrap)
- [![Solidity](https://www.bitdegree.org/learn/storage/media/images/439fb4c2-1d5e-4d96-a712-bc369bf5864a.svg) Solidity](https://www.bitdegree.org/learn/solidity)
- [![jQuery](https://www.bitdegree.org/learn/storage/media/images/c7f108e1-1c83-4ac0-b64a-103e5e231f9b.svg) jQuery](https://www.bitdegree.org/learn/jquery)
- [![Git](https://www.bitdegree.org/learn/storage/media/images/1da96abf-8c23-4fa3-b8d5-9b99e57c3e2e.svg) Git](https://www.bitdegree.org/learn/git)
- [![Chrome DevTools](https://www.bitdegree.org/learn/storage/media/images/91cfa58f-a3ad-487c-b4cf-9877bdbbdb09.svg) Chrome DevTools](https://www.bitdegree.org/learn/chrome-devtools)
- [![C++](https://www.bitdegree.org/learn/storage/media/images/20b7d886-6cb5-40ee-829e-aed4f3e1eccc.svg) C++](https://www.bitdegree.org/learn/c-plus-plus)
- [![Python](https://www.bitdegree.org/learn/storage/media/images/ddd73681-b989-42ef-95fe-4bd0dd2e7698.svg) Python](https://www.bitdegree.org/learn/python)
- [Other](https://www.bitdegree.org/learn/other)

[![BitDegree Learn](https://www.bitdegree.org/learn/assets/bitdegree-logo-white.svg)](https://www.bitdegree.org/learn/)

- [![HTML](https://www.bitdegree.org/learn/storage/media/images/96f8c890-c194-4dd3-a14e-912d5b185652.svg) HTML](https://www.bitdegree.org/learn/html)
- [![CSS](https://www.bitdegree.org/learn/storage/media/images/f58e503f-af9e-4ef7-85a5-1c7a32c74f02.svg) CSS](https://www.bitdegree.org/learn/css)
- [![PHP](https://www.bitdegree.org/learn/storage/media/images/93006417-fb6a-4196-8349-5c66420615cc.svg) PHP](https://www.bitdegree.org/learn/php)
- [![JavaScript](https://www.bitdegree.org/learn/storage/media/images/0179bd53-228d-43e8-9aa9-b89b29bd3a65.svg) JavaScript](https://www.bitdegree.org/learn/javascript)
- [![SQL](https://www.bitdegree.org/learn/storage/media/images/9c5aedcf-73e6-4884-8848-7c8fe9e7f396.svg) SQL](https://www.bitdegree.org/learn/sql)
- [![Bootstrap](https://www.bitdegree.org/learn/storage/media/images/6dc1de53-7104-4c21-a180-bf07d3fa938b.svg) Bootstrap](https://www.bitdegree.org/learn/bootstrap)
- [![Solidity](https://www.bitdegree.org/learn/storage/media/images/439fb4c2-1d5e-4d96-a712-bc369bf5864a.svg) Solidity](https://www.bitdegree.org/learn/solidity)
- [![jQuery](https://www.bitdegree.org/learn/storage/media/images/c7f108e1-1c83-4ac0-b64a-103e5e231f9b.svg) jQuery](https://www.bitdegree.org/learn/jquery)
- [![Git](https://www.bitdegree.org/learn/storage/media/images/1da96abf-8c23-4fa3-b8d5-9b99e57c3e2e.svg) Git](https://www.bitdegree.org/learn/git)
- [![Chrome DevTools](https://www.bitdegree.org/learn/storage/media/images/91cfa58f-a3ad-487c-b4cf-9877bdbbdb09.svg) Chrome DevTools](https://www.bitdegree.org/learn/chrome-devtools)
- [![C++](https://www.bitdegree.org/learn/storage/media/images/20b7d886-6cb5-40ee-829e-aed4f3e1eccc.svg) C++](https://www.bitdegree.org/learn/c-plus-plus)
- [![Python](https://www.bitdegree.org/learn/storage/media/images/ddd73681-b989-42ef-95fe-4bd0dd2e7698.svg) Python](https://www.bitdegree.org/learn/python)
- [Other](https://www.bitdegree.org/learn/other)
- [Sign up Free](https://www.bitdegree.org/connect)

- [Sign up Free](https://www.bitdegree.org/connect)

Code has been added to clipboard!

[Home](https://www.bitdegree.org/learn/) \| [Python](https://www.bitdegree.org/learn/python) \|Print

# Getting Started With Python Print: Outputting Messages

Reading time 3 min

Published Sep 23, 2019

Updated Oct 3, 2019

TL;DR – Python print() can be used to show a message in the Python terminal or to write a series of characters into an external file.

#### Contents

- [1. What the Python print() function does](#what-the-python-print-function-does)
- [2. How to print in Python](#how-to-print-in-python)
- [3. Printing to a file in Python](#printing-to-a-file-in-python)
- [4. Python 3 print() with a formatted output](#python-3-print-with-a-formatted-output)
- [5. Specifying end parameters when printing in Python](#specifying-end-parameters-when-printing-in-python)
- [6. Python print(): useful tips](#python-print-useful-tips)

## What the Python print() function does

Printing in Python normally involves **displaying** a message or **outputting** a result in the Python terminal. You can use `print()` with **one parameter**:

**Example** Copy

```hljs python
print(object)
```

However, `print()` operations work with **up to four parameters**:

**Example** Copy

```hljs python
print(object(s), sep = 'separator', end = '\n', file = sys.dout, flush = False)
```

The `objects(s)` are the objects which will be **printed**. The Python `print() function ` can take more than one of these. If **multiple objects** are present, then the `separator` tells Python what delimiter should be inserted **between each**.

Meanwhile, the `end` parameter allows you to insert a statement to be printed at the end of the output. `file` lets you write the output to a file. Finally, `flush` can forcibly output the stream following the `print()` operation.

`separator`, `end`, `file` and `flush` are all keywords. Therefore, if you want to **include these parameters** while printing in Python, you need to pay attention to their **standard** use.

Here's an example of using Python print() with several parameters:

**Example** Copy

```hljs python
print('x', '2', sep = ' is ')
```

[Try it Live](https://www.bitdegree.org/learn/best-code-editor/python-print-example-3) [Learn on Udacity](https://www.bitdegree.org/learn/goon/udacity-programming)

## How to print in Python

It’s easy to use the `print()` function in Python to print a **string directly**:

**Example** Copy

```hljs python
print('hello world')
```

[Try it Live](https://www.bitdegree.org/learn/best-code-editor/python-print-example-4) [Learn on Udacity](https://www.bitdegree.org/learn/goon/udacity-programming)

It’s equally easy to use `print()` to display the **value of an object**, as shown below:

**Example** Copy

```hljs python
a = 2
print('a', a, sep = ' is ')
```

[Try it Live](https://www.bitdegree.org/learn/best-code-editor/python-print-example-5) [Learn on Udacity](https://www.bitdegree.org/learn/goon/udacity-programming)

> **Note:** **print()** in Python 3 was updated significantly. This guide uses **print()** statements for Python 3.x rather than **print** commands of Python 2.x.

## Printing to a file in Python

If you **don’t specify** the `file` parameter when you call the `print()` command, Python will display text in the **terminal**.

However, if you use the `open` command to **load a file** in write mode prior to calling the Python `print()` function, characters and values can be written **directly** into it:

**Example** Copy

```hljs python
fl = open('file.txt', 'w')
print('This text will be written in the file', file = fl)
fl.close()
```

![DataCamp](https://assets.bitdegree.org/online-learning-platforms/storage/media/datacamp-logo-5f96bfc70113e.o.png)

**Pros**

- Easy to use with a learn-by-doing approach
- Offers quality content
- Gamified in-browser coding experience
- The price matches the quality
- Suitable for learners ranging from beginner to advanced

**Main Features**

- Free certificates of completion
- Focused on data science skills
- Flexible learning timetable

[GET 25% OFF](https://www.bitdegree.org/learn/goon/datacamp)

![Udacity](https://assets.bitdegree.org/online-learning-platforms/storage/media/udacity-review-logo-small.o.png)

**Pros**

- Simplistic design (no unnecessary information)
- High-quality courses (even the free ones)
- Variety of features

**Main Features**

- Nanodegree programs
- Suitable for enterprises
- Paid Certificates of completion

[UP TO 70% OFF](https://www.bitdegree.org/learn/goon/udacity)

![edX](https://assets.bitdegree.org/online-learning-platforms/storage/media/edx-logo-60190648f3a51.o.png?tr=w-200)

**Pros**

- A wide range of learning programs
- University-level courses
- Easy to navigate
- Verified certificates
- Free learning track available

**Main Features**

- University-level courses
- Suitable for enterprises
- Verified certificates of completion

[FREE COURSES](https://www.bitdegree.org/learn/goon/edx)

## Python 3 print() with a formatted output

A variety of characters can be used to **output specially formatted** text in Python. One frequently used example is `%s` which will tell Python to display a **numeric value** as a **string**:

**Example** Copy

```hljs python
pi = 3.1416
print('2 x Pi = %s' % (2 * pi))
```

[Try it Live](https://www.bitdegree.org/learn/best-code-editor/python-print-example-7) [Learn on Udacity](https://www.bitdegree.org/learn/goon/udacity-programming)

## Specifying end parameters when printing in Python

You may wish to position the printed information with **extra text** using the `end` keyword. You can also add extra strings or spaces, like in the example below where the `\n` (newline) character is used:

**Example** Copy

```hljs python
print('This line will be three line breaks above...', end = '\n\n\n')
print('... this line of text')
```

[Try it Live](https://www.bitdegree.org/learn/best-code-editor/python-print-example-8) [Learn on Udacity](https://www.bitdegree.org/learn/goon/udacity-programming)

The previous example code will create the output below:

**Example** Copy

```hljs python
This line will be three line breaks above...

... this line of text
```

## Python print(): useful tips

- The `+` operator, which can concatenate strings in Python, also **works within** `print()` operations.
- The `print` statement was **not a function** in Python 2. In Python 3, `print` became a function and **requires parentheses** to work.

[Previous Topic](https://www.bitdegree.org/learn/python-not-equal) [Next Topic](https://www.bitdegree.org/learn/python-queue)

###### Python

[Append](https://www.bitdegree.org/learn/python-append) [Array](https://www.bitdegree.org/learn/python-array) [Class](https://www.bitdegree.org/learn/python-class) [Command Line Arguments](https://www.bitdegree.org/learn/python-command-line-arguments) [Comment](https://www.bitdegree.org/learn/python-comment) [Enumerate](https://www.bitdegree.org/learn/python-enumerate) [Functions](https://www.bitdegree.org/learn/python-function) [Groupby](https://www.bitdegree.org/learn/pandas-groupby) [If... else](https://www.bitdegree.org/learn/python-if-else) [Map](https://www.bitdegree.org/learn/python-map) [Not Equal](https://www.bitdegree.org/learn/python-not-equal) [Print](https://www.bitdegree.org/learn/python-print) [Queue](https://www.bitdegree.org/learn/python-queue) [Random](https://www.bitdegree.org/learn/python-random) [Range](https://www.bitdegree.org/learn/python-range) [Round](https://www.bitdegree.org/learn/python-round) [Set](https://www.bitdegree.org/learn/python-set) [Sorting Lists](https://www.bitdegree.org/learn/python-sort-list) [Split](https://www.bitdegree.org/learn/python-split) [Time](https://www.bitdegree.org/learn/python-time) [Train\_test\_split](https://www.bitdegree.org/learn/train-test-split) [Variables](https://www.bitdegree.org/learn/python-global-variables) [While Loop](https://www.bitdegree.org/learn/while-loop-python)

##### Best-rated MOOCs to Learn Programming:

![DataCamp Review](https://assets.bitdegree.org/online-learning-platforms/storage/media/datacamp-logo-5f96bfc70113e.png)

9.8

[Read review](https://www.bitdegree.org/online-learning-platforms/datacamp-review)

![Udacity Review](https://assets.bitdegree.org/online-learning-platforms/storage/media/udacity-review-logo-small.png)

9.6

[Read review](https://www.bitdegree.org/online-learning-platforms/udacity-review)

![edX Review](https://assets.bitdegree.org/online-learning-platforms/storage/media/edx-logo-60190648f3a51.o.png)

9.4

[Read review](https://www.bitdegree.org/online-learning-platforms/edx-review)

#### Related Posts

[**Guide on the Python Map Function for Applying Functions to Sequences** \
Find out the uses of Python map function to apply functions to objects in sequences. This tutorial on Python map focuses on lists, tuples, sets and more!\
\
4 min read\
\
8 examples](https://www.bitdegree.org/learn/python-map)

[**A Complete Guide to the Python Split Function** \
This tutorial on Python split() covers how .split in Python works. Learn from simple code examples to use Python split and divide string variables.\
\
3 min read\
\
6 examples](https://www.bitdegree.org/learn/python-split)

[**Looping With the Python Enumerate Function** \
This guide on Python enumerate() reveals what does enumerate do in Python. You will learn how to loop through iterable objects with Python enumerate.\
\
3 min read\
\
7 examples](https://www.bitdegree.org/learn/python-enumerate)

## Related Code Examples

[**Python**](https://www.bitdegree.org/learn/best-code-editor/python-append-example-1)

[Python Append Example 1](https://www.bitdegree.org/learn/best-code-editor/python-append-example-1)

[**Python**](https://www.bitdegree.org/learn/best-code-editor/while-loop-python-example-2)

[While Loop Python Expressions](https://www.bitdegree.org/learn/best-code-editor/while-loop-python-example-2)

[**Python**](https://www.bitdegree.org/learn/best-code-editor/python-if-else-example-2)

[The Use of the Python If Else Statement](https://www.bitdegree.org/learn/best-code-editor/python-if-else-example-2)

[**Python**](https://www.bitdegree.org/learn/best-code-editor/python-class-example-6)

[Complex Python Class Objects](https://www.bitdegree.org/learn/best-code-editor/python-class-example-6)

[**Python**](https://www.bitdegree.org/learn/best-code-editor/python-function-example-4)

[Setting Default Values for Python Function Parameters](https://www.bitdegree.org/learn/best-code-editor/python-function-example-4)

[**Python**](https://www.bitdegree.org/learn/best-code-editor/python-not-equal-example-3)

[Python Not Equal Example 3](https://www.bitdegree.org/learn/best-code-editor/python-not-equal-example-3)

##### Learn

- [Programming Languages](https://www.bitdegree.org/learn/)
- [Get Certified](https://www.bitdegree.org/free-certifications-online)
- [Sitemap](https://www.bitdegree.org/learn/sitemap)
- [Code Examples](https://www.bitdegree.org/learn/code-examples)

##### Web Development

- [HTML](https://www.bitdegree.org/learn/html)
- [CSS](https://www.bitdegree.org/learn/css)
- [JavaScript](https://www.bitdegree.org/learn/javascript)

##### Server Side

- [PHP](https://www.bitdegree.org/learn/php)
- [SQL](https://www.bitdegree.org/learn/sql)

##### Tools

- [Code Editor](https://www.bitdegree.org/learn/best-code-editor)
- [Color Picker](https://www.bitdegree.org/learn/color-scale)
- [Devtools](https://www.bitdegree.org/learn/chrome-devtools)
- [Git](https://www.bitdegree.org/learn/git)

##### Courses

- [Video Courses](https://www.bitdegree.org/courses/)
- [Code Theory](https://www.bitdegree.org/learn/)
- [Tutorials](https://www.bitdegree.org/tutorials/)
- [Free Certifications Online](https://www.bitdegree.org/free-certifications-online)
- [Online Learning Platforms](https://www.bitdegree.org/online-learning-platforms)
- [Learning Paths](https://www.bitdegree.org/courses/learning-paths)
- [Apply for a Scholarship](https://www.bitdegree.org/courses/scholarships)

##### Company

- [About BitDegree](https://www.bitdegree.org/about-us)
- [EU Projects](https://www.bitdegree.org/about-us/eu-projects)
- [Hiring](https://www.bitdegree.org/hiring)
- [Blog](https://blog.bitdegree.org/)
- [BitDegree Reviews](https://www.bitdegree.org/bitdegree-reviews)
- [FAQ](https://www.bitdegree.org/faq)

##### Legal

- [Policies & Legal Documents](https://www.bitdegree.org/policies)
- [Privacy Policy](https://www.bitdegree.org/privacy-policy)
- [Cookie Policy](https://www.bitdegree.org/cookie-policy)
- [Refund Policy](https://www.bitdegree.org/refund-policy)
- [Terms of Service](https://www.bitdegree.org/terms-of-service)

##### Instructors & Partners

- [Advertise on BitDegree](https://www.bitdegree.org/advertise)
- [BitDegree Studio](https://www.bitdegree.org/courses/studio)
- [Become Our Partner](/cdn-cgi/l/email-protection#c2b2a3b0b6aca7b0b182a0abb6a6a7a5b0a7a7ecadb0a5)
- [Hire Talents With Us](https://www.bitdegree.org/courses/partners)
- [Learning platforms' reviews](https://www.bitdegree.org/online-learning-platforms)

##### Follow Us

[X](https://x.com/intent/follow?screen_name=BitDegree)[Discord](https://discord.gg/NXNN5tjA7g)[Telegram](https://t.me/BitDegree)[YouTube](https://www.youtube.com/c/BitDegree_org?sub_confirmation=1)[Facebook](https://www.facebook.com/bitdegree.org)[LinkedIn](https://www.linkedin.com/company/bitdegree)[Medium](https://blog.bitdegree.org)[Instagram](https://www.instagram.com/bitdegree/)[Reddit](https://www.reddit.com/r/bitdegree)[GitHub](https://github.com/bitdegree)

Learn to earn: BitDegree free online courses give you the best online education with a gamified experience. Gain knowledge and get your dream job: learn to earn.

[![BitDegree logo](https://cdn.bitdegree.org/assets/bitdegree-logo-white.svg)](https://www.bitdegree.org)

Copyright © 2024 BitDegree.org \| [\[email protected\]](/cdn-cgi/l/email-protection#e088858c8c8fa0828994848587928585ce8f9287)

[![EXCLUSIVE OFFER: GET 25% OFF](https://assets.bitdegree.org/online-learning-platforms/storage/media/datacamp-logo-white.o.png)](https://www.bitdegree.org/goon/datacamp-exclusive-promo)

[**EXCLUSIVE OFFER: GET 25% OFF** Save Big With DataCamp Promo Code](https://www.bitdegree.org/goon/datacamp-exclusive-promo)

Days

Hours

Minutes

Seconds

[GET 25% OFF](https://www.bitdegree.org/goon/datacamp-exclusive-promo)