turtle.write() function in Python

The turtle.write() function writes text at the turtle’s current position. The font, alignment, and other properties can be customized.

Syntax: turtle.write(arg, move=False, align=”left”, font=(“Arial”, 8, “normal”))
Parameters: arg – the object to be written (converted to string); move – if True, the turtle moves to the bottom-right of the text; align – “left”, “center”, or “right”; font – a tuple (fontname, fontsize, fonttype).

Let us see an example to implement the turtle.write() function in Python:

Demo23.py

# turtle.write() function in Python
# Code by Studyopedia

import turtle

window = turtle.Screen()
t = turtle.Turtle()

t.write("Hello, Turtle!", font=("Arial", 16, "normal"))
t.forward(100)
t.write("This is fun!", font=("Arial", 12, "italic"))

window.exitonclick()

The following is the output:

turtle.write() function in Python

In the above code, we followed the below steps:

  1. Import the module: import turtle loads Python’s turtle graphics library.
  2. Create the screen: window = turtle.Screen() opens the drawing window.
  3. Create the turtle: t = turtle.Turtle() creates the turtle (your pen).
  4. Write first text: t.write(“Hello, Turtle!”, font=(“Arial”, 16, “normal”)) draws the string at the turtle’s current position using Arial, size 16, normal style. The turtle doesn’t move unless you pass move=True; default alignment is left.
  5. Move forward: t.forward(100) moves 100 units, drawing a line if the pen is down (it is by default).
  6. Write second text: t.write(“This is fun!”, font=(“Arial”, 12, “italic”)) writes the new string at the new position with Arial, size 12, italic.
  7. Close on click: window.exitonclick() keeps the window open until you click, then closes.

If you liked the tutorial, spread the word and share the link and our website, Studyopedia, with others.


For Videos, Join Our YouTube Channel: Join Now


Read More:

turtle.pen() function in Python
turtle.color() function in Python
Studyopedia Editorial Staff
[email protected]

We work to create programming tutorials for all.

No Comments

Post A Comment