当前位置: 首页 > 图灵资讯 > 行业资讯> Python center()方法

Python center()方法

发布时间:2025-10-15 21:17:35

描述

Python center() 回到原字符串中间,用空间填充到长度 width 新字符串。默认填充字符为空格。

语法

center()方法语法:

string.center(width[,fillchar])

参数

width -- 字符串的总宽度。

fillchar -- 填充字符。

返回值

该方法返回原字符串,并用空间填充到长度 width 新字符串。

实例

以下实例展示了center()方法的实例:

#!/usr/bin/python

string="thisisstringexample...wow!!!";

print"string.center(40,'a'):",string.center(40,'a')

上述实例的输出结果如下:

string.center(40,'a'):aaaathisis

相关文章

python如何通过日志分析加入黑名单

python如何通过日志分析加入黑名单

2025-10-17
python抽象基类之_subclasshook_方法

python抽象基类之_subclasshook_方法

2025-10-17
Python3 面向对象

Python3 面向对象

2025-10-17
Python中可迭代对象、迭代器详解

Python中可迭代对象、迭代器详解

2025-10-17
Python3 XML解析

Python3 XML解析

2025-10-17
Python之类方法、__new__方法和__init__方法介绍

Python之类方法、__new__方法和__init__方法介绍

2025-10-17