Python Turtle Write Function

In this Python tutorial, we will learn How to Write functions in Python Turtle and we will also cover different examples related to the Python Turtle Write Function. And we will cover these topics.

  • Python Turtle Write Function
  • Python Turtle Write Name
  • Python Turtle Write Position
  • Python Turtle Write Font size
  • Python Turtle Write Color
  • Python Turtle Write Align
  • Python Turtle Write Variable
  • Python Turtle Write Text size
  • Python Turtle Write On-Screen
  • Python Turtle Write Text

Python Turtle Write Function

In this section, we will learn about how to write a function in Python turtle.

Write function is used for writing the text anywhere. If the user wants to write something on screen then they simply use the write() function.

Code:

In the following code, we use the write function to write text on the screen.

turtle.write()-It is used to write text anywhere users want.


import turtle 


ws=turtle.write("Python Guides" , move=True,align='right',font=('Arial',15,'bold'))
turtle.done()

Output:

After running the above code we get the following output in which we can see the text is written on the screen with the write() function.

Python turtle write function
Python turtle write function Output

Read Python Turtle Circle

Python Turtle Write Name

In this section, we will learn about how to write name in Python turtle.

We can write any name of our choice anywhere from tur.write() function. We can also rewrite the name if we won’t simply remove the previous name and write the new name in the argument.

Code:

In the following code, we import the turtle library from turtle import *, import turtle as tur, and use the write()function to write name on the screen and also give “cyan” color to name.

from turtle import *
import turtle as tur

tur.color('cyan')
style = ('Courier', 30, 'bold')
tur.write('PythonGuides', font=style, align='center')
tur.hideturtle()
tur.done()

Output:

In the following output, we see the name is shown on the screen which is written with the help of the write()function.

Python turtle write name
Python turtle write name Output

Read Python Turtle Speed With Examples

Python Turtle Write Position

In this section, we will learn about how to set write positions in Python turtle.

Write position is that when we write text, name, or anything else and now we want to change the position or we can say set the position of this text to left, right, or center.

Code:

In this code, we import turtle library from turtle import *, import turtle, and also import date, time library for showing the current date and time.

READ:  PyTorch nn linear + Examples

today1 = datetime.datetime.now() it is used for showing current date and time.

from turtle import *
import turtle
import datetime

tur = turtle.Turtle()

today1 = datetime.datetime.now()
tur.hideturtle()
tur.penup()

tur.backward((tur.getscreen().window_width() / 2) - 10)
message = "Hello Welcome to Python Guides! \nToday is " + today1.strftime("%A") + ', ' + today1.strftime("%d") \
           + ', ' + today1.strftime("%B") + ', ' + today1.strftime("%Y") 

tur.write(message,move=False, font=('arial',10,'bold'),align='left')
turtle.exitonclick()

Output:

After running the above code, we get the following output in which we see the text with the current date and time is placed at the left position on the screen.

Python turtle write position
Python turtle write position Output

Read Python Turtle Colors

Python Turtle Write Font size

In this section, we will learn about how to set the font size in Python turtle.

Font size is how large our text is display on the screen. We can also change the font size as per the requirement. If we think the small text size looks pretty then change the font size.

Code:

In the following code, we import the turtle library and write text with the help of the write() function and also set font size as font=(“Times New Roman”, 15, “bold”)).

import turtle

fontsize = turtle.Turtle()

fontsize.write("Welcome To Python Guides", font=("Times New Roman", 15, "bold"))
turtle.done()

Output:

In the following output, we see the text is written on the screen with a font size of 15.

Python turtle write font size
Python turtle write font-size output

Read How to attach an image in Turtle Python

Python Turtle Write Color

In this section, we will learn how to give color to text in Python turtle.

Write color is to giving color to text. When we write text, name, or anything else their default color is black if we want to change the color then we use tur.color(“cyan).

Code:

In the following code, we import the turtle library for writing the text and giving color to the text.

  • turt.begin_fill() is used for start filling color.
  • turt.color(‘cyan’) is used to fill the color in the shape.
  • turt.forward(50) is used for moving the turtle in the forward direction.
  • turt.left(90) is used for moving the turtle in the left direction.
  • turt.end_fill() is used to end the filling color.
  • turt.write(‘Python Guides’) is used for showing the text on the screen.
  • turt.fillcolor(‘blue’) is used for give the color to text.
import turtle


def turt():
    turt = turtle.Turtle()
    turt.fillcolor('blue')
    turt.begin_fill()
    turt.color('cyan')
    for i in range(4):
        turt.forward(50)
        turt.left(90)
    turt.end_fill()
    
    
def screen():
    turt = turtle.Turtle()
    turt.color('blue')
    turt.fillcolor('cyan')
    turt.begin_fill()
    turt.penup()
    turt.goto(0, -20)
    turt.pendown()
    turt.write('Python Guides')
    turt.end_fill()
    
turt()
screen()
turtle.done()

Output:

After running the above code we get the following output in which we see a shape and text is shown on the screen

Python turtle write color
Python turtle write color Output

Read Python Turtle Commands

Python Turtle Write Align

In this section, we will learn about how to align the text in Python turtle.

READ:  Matplotlib is currently using agg a non-gui backend

As per the page requirement, we can give align to text, normally alignment of the page start from left to right. If we want to change the alignment then we can give text-align center or align-right then the page start from the center to right or right to left.

Code:

In the following code, we import the turtle library from turtle import Turtle and also give the font to text.

  • Turtle()-It is a pre-installed library that is used for creating shapes and pictures.
  • turtle.penup()- It stops drawing of the turtle pen.
  • turtle.home() function is used to move the turtle to the origin.
  • turtle.write()- It is used to write the text on the screen.
from turtle import Turtle, Screen

FONTSIZE = 30

FONT = ("Arial", FONTSIZE, "bold")

turtle = Turtle()

turtle.penup()
turtle.home()

turtle.sety(-FONTSIZE/2)

turtle.write("Heya Python Guides", align="center", font=FONT)

turtle.hideturtle()

screen = Screen()

screen.exitonclick()

Output:

After running the above code we get the following output in which we see the text is aligned in the center of the page.

Python turtle write align
Python turtle write align Output

Read How to Create a Snake game in Python using Turtle

Python Turtle Write Variable

In this section, we will learn about how to write a turtle variable in a python turtle.

Variable is the memory location where we store values. Write variable is defined as when we draw some shapes like a circle we can control how big our circle is then we assign the value to a variable for that and we also write some text inside the shape.

Code:

In the following code, we import turtle library import turtle as tur for drawing a circle.

  • tur.dot() is used to place a dot inside the circle.
  • tur.penup() It stops drawing of the turtle pen.
  • tur.goto(0, -radius) It draws the circle from origin to given radius.
  • tur.pendown() It starts drawing of the turtle pen.
  • tur.write()is used for writing the text inside the circle.
import turtle as tur
import math
radius = int( input ( "Enter number: " ) ) 
Area = (( math.pi ) * (radius ** 2))   

tur.dot() 
tur.penup() 
tur.goto(0, -radius) 
tur.pendown() 
tur.circle(radius) 
tur.penup()  
tur.home() 

tur.hideturtle() 
tur.write("Area of circle", (Area))
tur.exitonclick()

Output:

After running the above code we get the following output in which we see the circle is drawn and the text is placed inside the circle.

Python turtle write variable
Python turtle write variable Output

Read Draw colored filled shapes using Python Turtle

Python Turtle Write Text size

In this section, we will learn how to change the write text size in Python turtle.

Text size is similar to the font size as we change the font number to see how the text looks. As we increase the font size the size of the text also increases and if we decrease the font size the text size also decreases.

READ:  Django CRUD with Bootstrap Template

Code:

In the following code, we import turtle library import turtle and we want to resize the text size.

textsize.write( “Python Guides”, font=(“Arial”, 20, “bold”) ) It is use to write the text after writing the text we can give the text size.

import turtle

textsize = turtle.Turtle()

textsize.write("Python Guides", font=("Arial", 20, "bold"))
turtle.done()

Output:

After running the above code we get the following output in which we see the text is shown on the screen with the given text size.

Python turtle write text size
Python turtle write text size Output

Read How to draw a shape in python using Turtle

Python Turtle Write On-Screen

In this section, we will learn about how to write on-screen in Python turtle.

We can write anything like a text, paragraph on-screen by simply using the write() function.

Code:

In the following code, we import turtle package import turtle as turt for writing the text and also give styling to text.

turt.write(“Hello Guides”, font=(“Verdana”, 20, “normal”)) it used for writing the text for giving styling to text we use verdana.

from turtle import *
import turtle as turt

turt.write("Hello Guides", font=("Verdana",20, "normal"))
turt.exitonclick()

Output:

After running the above code we get the following output in which we write is displayed on-screen.

Python turtle write on screen
Python turtle write on-screen output

Also, Read: Python Turtle Art

Python Turtle Write Text

In this section, we will learn about how to write text in Python turtle.

We can write text on paper, a notebook, textbook with the help of a pen and pencil. In this, we see how we can write the text on a screen with the help of the turtle at the current turtle position.

Code:

In the following code, we import the turtle package import turtle as turt for writing a text on the screen. The turtle writes text is written with the help of the write() function.

import turtle as turt

turt.write("Welcome guides", move=True)
turt.exitonclick()

Output:

After running the above code we get the following output in which we see the text is written on the screen.

Python turtle write text
Python turtle write text output

You may like the following Python tutorials:

So, in this tutorial, we discussed the Python turtle write function and we have also covered different examples related to its implementation. Here is the list of examples that we covered.

  • Python Turtle Write Function
  • Python Turtle Write Name
  • Python Turtle Write Position
  • Python Turtle Write Font size
  • Python Turtle Write Color
  • Python Turtle Write Align
  • Python Turtle Write Variable
  • Python Turtle Write Text size
  • Python Turtle Write On-Screen
  • Python Turtle Write Text