Example:
import pygame #importing the pygame library
pygame.init() #intialising the pygame modules.
disp_surface = pygame.display.set_mode((400, 400))
font = pygame.font.Font('chalkduster.ttf', 32) #first attribute is the font file, and the #second is the font size.
text_obj = font.render('Sample Text', True) #creating a text object
rect_obj = text.get_rect() #creating the rectangle object
rect_obj.center = (X // 2, Y // 2) #positioning the rectangle object
while True:
disp_surface.blit(text_obj, rect_obj)
pygame.display.update() #displaying the display surface.

You can also try this code with Online Python Compiler
Run Code
Output: We will get “Sample Text “at the center of the application.
Editing the text using KeyBoard
To edit the text using KeyBoard will be simple in Pygame. First, we need to create a text variable that is used to store a string value. Then, we need to render this text object to an image using the render() method.
After that, we need to create a rectangle object by using the get_rect() method. And then, we can set the position of this created rectangle object on the display surface. Then, we can use event.get() method to control the keyboard input. Example:
if event.type == KEYDOWN:
if event.key == K_BACKSPACE:
if len(text)>0:
text = text[:-1]
else:
text += event.unicode

You can also try this code with Online Python Compiler
Run Code
And then, we need to render the text again. And Finally, we need to update the display using the display.update() method in Pygame.
In this way, we can work with text in Pygame. There are many more functionalities that we can play with text in Pygame. We will explore them in the later blogs.
Frequently Asked Questions
What is Pygame?
Pygame is a python programming language library that allows developers to develop applications on gaming themes. It is a well-developed library with many valuable functions to implement several functionalities of our application.
How to write text in Pygame?
We can not be able to write text on Pygame directly, and we need to create particular valuable objects in the Pygame application before writing the text on the screen. The valuable objects are text objects, display objects, rectangle objects, etc.
How to change the size of the font in Pygame?
In Pygame applications, once the text or font is created, then the size of the font cannot be changed. It should always be taken care of at the start of the initialization.
What is the best text editor for Pygame Programming?
There are a lot of text editors that support scripting the pygame programs. One of the editors that ease the pygame application building process is EzText Editor. This will effortlessly allow text inputs to our Pygame application.
How to create a rectangle object for text input in Pygame?
In Pygame, once the text object is generated, we will use the Font object in order to create a rectangle object in order to display text on it. We will use the get_rect() method in Pygame to do so.
Conclusion
In this current article, we have extensively discussed the text in Pygame. We started with a brief introduction to working with text in Pygame, then discussed editing the text using Keyboard. Coding Ninjas has you covered. To learn, see Operating System, Unix File System, File System Routing, and File Input/Output.
Refer to our most valuable Guided Path on Coding Ninjas Studio to upskill yourself in Data Structures and Algorithms, JavaScript, System Design, Competitive Programming, and many more! If you want to test your own competency in the coding domain, you may check to participate in the contests and mock test series hosted on Coding Ninjas Studio! But suppose you have just started your learning process and are looking for questions asked by tech giants like Amazon, Microsoft, Uber, etc. In that case, you must look at the problems, interview experiences, and interview bundle for placement preparations.
Nevertheless, you may consider our paid courses to give your career an edge over others!
Do upvote our blogs if you find them helpful and engaging!
Happy Learning!