easy +8 pts

Center a string in a width

Learn to pad a string symmetrically with spaces to a given width.

Write a function `center_string(text: str, width: int) -> str` that returns a new string of exactly `width` characters. The input `text` is centered within this width: extra spaces are added equally to the left and right. If the number of spaces needed is odd, the extra space goes on the **left**. If `width` is less than or equal to the length of `text`, the function returns `text` unchanged. You may assume `width` is a non-negative integer and `text` is a string.

Constraints

- `width` is a non-negative integer (0 <= width <= 1000) - `len(text) <= 1000` - The function must return a string of length `max(len(text), width)`.

Example

```python
>>> center_string('hello', 11)
'   hello   '
>>> center_string('hello', 10)
'   hello  '
>>> center_string('hello', 5)
'hello'
>>> center_string('', 4)
'    '
```
8 points ~10 min

Recent Submissions

No submissions yet — hit Run Tests to try!

Hints

Calculate the total padding needed: `total_pad = width - len(text)`.
If total padding is odd, the extra space goes on the left, so `left = (total_pad + 1) // 2` and `right = total_pad // 2`.
Use string concatenation or `str.ljust`/`str.rjust` wisely.
Remember to handle the case where width is smaller or equal to text length.
Python 3
All tests passed!
Test Results
Press Ctrl+Enter or click Run Tests to execute your code.