string.center
时间: 2023-10-02 18:08:46 浏览: 43
The `center` method is a built-in Python string method that returns a centered string. It takes two arguments: the first argument specifies the width of the resulting string, and the second argument is the fill character (default is a space) that is used to fill the remaining space on the left and right sides of the original string.
Here's the syntax:
```python
string.center(width[, fillchar])
```
Here's an example:
```python
string = "hello"
centered_string = string.center(10, '-')
print(centered_string) # output: --hello---
```
In this example, the resulting string has a width of 10 characters and is centered by adding three dashes on the left and three dashes on the right of the original string "hello".