用center方法写
时间: 2023-08-04 13:11:02 浏览: 41
如果您想要使用Python的字符串方法`center()`,可以像下面这样写:
```python
text = "Hello World"
centered_text = text.center(20, "*")
print(centered_text)
```
这段代码会将`text`字符串居中,并用`*`字符进行填充,让它成为长度为20的新字符串。输出结果如下:
```
****Hello World*****
```
`center()`方法的第一个参数是新字符串的长度,第二个参数是用于填充的字符,默认为`空格`。如果要居中的文本长度超过指定的长度,则`center()`方法不会进行截断,而是返回原始字符串。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![7z](https://img-home.csdnimg.cn/images/20210720083312.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![xlsx](https://img-home.csdnimg.cn/images/20210720083732.png)
![xlsx](https://img-home.csdnimg.cn/images/20210720083732.png)
![pptx](https://img-home.csdnimg.cn/images/20210720083543.png)