Remember
center() pads text on both sides to reach a minimum width. Text longer than that width is not truncated.
center() pads text on both sides to reach a minimum width. Text longer than that width is not truncated.
Follow the example, change its sample, and compare what happens.
Explain the result and test your understanding.
Focus on how the names, objects, and calculations change. We’ll inspect the printed output in the next section.
Follow the code and watch its state change. Guided simulation.
text = 'tea'
result = text.center(8, ".")
print(result)Top-level scope
'tea'Object 2 · str'..tea...'center() pads text on both sides to reach a minimum width. Text longer than that width is not truncated.
(no output)
What do you expect this program to print?
EXAMPLE INSTRUCTIONS
text = 'tea'
result = text.center(8, ".")
print(result)
|
v
DISPLAYED RESULT
..tea...
FINAL BINDINGS
text --> object 1 (str)
result --> object 2 (str)
object 1: 'tea'
object 2: '..tea...'Choose a different sample input, then follow every step through to the output. Before revealing it, predict what the program will display.
Choose a sample and follow its execution. Guided simulation.
text = 'tea'
result = text.center(8, ".")
print(result)Top-level scope
'tea'Object 2 · str'..tea...'The example has finished. Compare the final output with the values and references above.
..tea...
What do you expect this program to print?
text = 'tea'
result = text.center(8, ".")
print(result)String center() MethodThe center() method in Python returns a new string of a given length with the original string center-aligned, and padded with spaces or a specified fill character on both sides.
string.center(width, fillchar=' ')
width – Total length of the final string (including the original string and padding)fillchar (optional) – The character used for padding (default is space)text = "Python"
centered = text.center(10)
print(centered)
Python
Why? The original string is 6 characters long. To make it 10 characters wide, 2 spaces are added to each side.
text = "Python"
centered = text.center(12, '*')
print(centered)
***Python***
Note: If the number of padding characters is odd, the extra character is placed on the right side.
Centering is often used when displaying titles or headers in reports or CLI tools.
title = "Welcome"
print(title.center(30, '='))
===========Welcome===========
fillchar will raise a TypeErrorwidth will also raise an errorIf asked to format text output in coding interviews, methods like center(), ljust(), and rjust() are great tools to mention.
center() centers a string in a field of a given widthWrite a program that takes a user's name and displays it centered within a line of asterisks:
name = input("Enter your name: ")
print(name.center(30, '*'))
Connect what you noticed in the example with the idea below.
center() pads text on both sides to reach a minimum width. Text longer than that width is not truncated.
center() truncates text that is longer than the width.
This is the misconception to avoid.Reference checks are optional and do not count toward course progress.