I am trying to position some text on my screen using pygame, and knowing the size of the string would help.

When using C# and XNA I could use SpriteFont.MeasureString("Hello World") to get the size of the rendered string.

Is there any way to do something similar in pygame?


1 Answer 1


You can use the size method

size(text) -> (width, height)

Returns the dimensions needed to render the text. This can be used to help determine the positioning needed for text before it isrendered. It can also be used for wordwrapping and other layout effects.

Here is an example

myFont = pygame.font.Font(None, fontSize)

width  = myFont.size("Hello World")[0]
heigth = myFont.size("Hello World")[1]

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .