Courseiva
Strings →easyMultiple Choice

PCAP Strings Practice Question

A developer is formatting a log message and wants to ensure that a string variable `name` is centered within a field of width 20, padded with asterisks (`*`) on both sides. For example, if `name = "Alice"`, the result should be `*******Alice********` (7 asterisks on the left, 8 on the right). Which method call achieves this?

⚠ Common exam trap

The trap here is mixing up the justification methods and forgetting that center() places extra padding on the right when the total padding is odd.

Answer choices

Why each option matters

Answer the question above first, then reveal the full breakdown to understand why each option is right or wrong.

Correct answer & explanation

✓

name.center(20, '*')

To center a string within a field and pad with a specific character, the str.center() method is used. It takes the desired width and an optional fill character. When the total padding is odd, Python places the extra character on the right, matching the example. The other methods either left-justify, right-justify, or pad with zeros, none of which produce a centered result with asterisks on both sides.

Answer analysis

Option-by-option breakdown

For each option: why learners choose it and why it is or isn't the right answer here.

  • ✗

    name.rjust(20, '*')

    Why it's wrong here

    str.rjust(width, fillchar) right-justifies the string, padding only on the left. For 'Alice', it would produce '***************Alice', with all asterisks on the left and none on the right. This does not center the string, so it does not meet the requirement of asterisks on both sides.

  • ✗

    name.ljust(20, '*')

    Why it's wrong here

    str.ljust(width, fillchar) left-justifies the string, padding only on the right. For 'Alice', it would produce 'Alice***************', with all asterisks on the right and none on the left. This does not center the string, so it fails the requirement of having asterisks on both sides.

  • ✓

    name.center(20, '*')

    Why this is correct

    The str.center(width, fillchar) method returns a new string of length width, with the original string centered and padded with the specified fill character. For 'Alice' and width 20, the total padding is 15 characters. Python puts the extra padding on the right when the padding is odd, resulting in 7 asterisks on the left and 8 on the right, exactly as required.

  • ✗

    name.zfill(20)

    Why it's wrong here

    str.zfill(width) pads the string on the left with zeros ('0') to reach the specified width. It does not center the string and does not use a custom fill character. For 'Alice', it would produce '000000000000000Alice', which is not centered and uses zeros instead of asterisks, so it is completely wrong for this scenario.

About these practice questions

One of 421 original PCAP practice questions on Courseiva, each with a full explanation and wrong-answer analysis — not exam dumps or protected exam content. Learn why practice questions differ from exam dumps →

How Courseiva writes practice questions · Editorial policy

JA

Written and reviewed by Johnson Ajibi, MSc IT Security

Senior Network & Security Engineer · founder of Courseiva

Last reviewed September 2026 · checked against the official Python Institute exam blueprint

This PCAP practice question is part of Courseiva's free Python Institute certification practice question bank. Courseiva provides original exam-style practice questions with explanations, topic-based practice, mock exams, readiness tracking, and study analytics to help learners prepare for the PCAP exam.