菜鸟教程 -- 学的不仅是技术,更是梦想!

Python 3 教程
Python3 教程 Python3 简介 Python3 环境搭建 Python3 VScode Python3 基础语法 Python3 基本数据类型 Python3 数据类型转换 Python3 解释器 Python3 注释 Python3 运算符 Python3 数字(Number) Python3 字符串 Python3 列表 Python3 元组 Python3 字典 Python3 集合 Python3 条件控制 Python3 循环语句 Python3 编程第一步 Python3 推导式 Python3 迭代器与生成器 Python3 with Python3 函数 Python3 lambda Python3 装饰器 Python3 数据结构 Python3 模块 Python __name__ Python3 输入和输出 Python3 File Python3 OS Python3 错误和异常 Python3 面向对象 Python3 命名空间/作用域 Python 虚拟环境的创建 Python 类型注解 Python3 标准库概览 Python3 实例 Python 测验

Python3 高级教程

Python3 正则表达式 Python3 CGI编程 Python3 MySQL(mysql-connector) Python3 MySQL(PyMySQL) Python3 网络编程 Python3 SMTP发送邮件 Python3 多线程 Python3 XML 解析 Python3 JSON Python3 日期和时间 Python3 内置函数 Python3 MongoDB Python3 urllib Python uWSGI 安装配置 Python3 pip Python3 operator Python math Python requests Python random Python OpenAI Python 有用的资源 Python AI 绘画 Python statistics Python hashlib Python 量化 Python pyecharts Python selenium 库 Python 爬虫 Python Scrapy 库 Python Markdown Python sys 模块 Python Pickle 模块 Python subprocess 模块 Python queue 模块 Python StringIO 模块 Python logging 模块 Python datetime 模块 Python re 模块 Python csv 模块 Python threading 模块 Python asyncio 模块 Python PyQt Python for 循环 Python while 循环
(追記) (追記ここまで)

Python3 center()方法

Python3 字符串 Python3 字符串

center() 方法返回一个指定的宽度 width 居中的字符串,fillchar 为填充的字符,默认为空格。

语法

center()方法语法:

str.center(width[, fillchar])

参数

  • width -- 字符串的总宽度。
  • fillchar -- 填充字符。

返回值

返回一个指定的宽度 width 居中的字符串,如果 width 小于字符串宽度直接返回字符串,否则使用 fillchar 去填充。

实例

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

实例

#!/usr/bin/python3

str = "[runoob]"

print ("str.center(40, '*') : ", str.center(40, '*'))

以上实例输出结果如下:

str.center(40, '*') : ****************[runoob]****************

Python3 字符串 Python3 字符串

AI 思考中...

2 篇笔记 写笔记

  1. #0

    好好学习天天向上

    522***[email protected]

    180

    str.center(width[, fillchar]) 函数:

    1、如果 width 小于字符串宽度直接返回字符串,不会截断:

    >>> str = "[www.runoob.com]"
    >>> print ("str.center(4, '*') : ", str.center(4, '*'))
    str.center(4, '*') : [www.runoob.com] // width 小于字符串宽度
    >>>

    2、fillchar 默认是空格

    >>> str = "[www.runoob.com]"
    >>> print ("str.center(40) : ", str.center(40))
    str.center(40) : [www.runoob.com] // fillchar默认是空格

    3、fillchar 只能是单个字符

    >>> str = "[www.runoob.com]"
    >>> print ("str.center(40, '?!') : ", str.center(40, '?!'))
    Traceback (most recent call last):
     File "<stdin>", line 1, in <module>
    TypeError: The fill character must be exactly one character long

    好好学习天天向上

    522***[email protected]

    8年前 (2018年03月02日)
  2. #0

    m5320dixm

    men***[email protected]

    113

    补充说明一下:

    print('123'.center(2, '*'))
    # 123
    print('123'.center(3, '*'))
    # 123
    print('123'.center(4, '*')) # 奇数个字符时优先向右边补*
    # 123*
    print('123'.center(5, '*'))
    # *123*
    print('1234'.center(4, '*'))
    # 1234
    print('1234'.center(5, '*')) # 偶数个字符时优先向左边补*
    # *1234
    print('1234'.center(6, '*'))
    # *1234*
    print('1234'.center(7, '*'))
    # **1234*

    m5320dixm

    men***[email protected]

    5年前 (2021年02月08日)

点我分享笔记

  • 昵称 (必填)
  • 邮箱 (必填)
  • 引用地址

AltStyle によって変換されたページ (->オリジナル) /