Weibw's World Weibw's World
首页
  • HTML
  • Python

    • Python基础知识
    • Python CookBook第三版
    • Flask
  • MySQL

    • MySQL基础知识
    • MySQL调优
    • MySQL面试题
算法
  • FineReport
  • Kettle
  • Git
  • 微信公众号文章
  • 优秀博客文章
  • 其他
收藏夹
  • 分类
  • 标签
  • 归档
GitHub (opens new window)

Weibw

一个没有梦想的咸鱼
首页
  • HTML
  • Python

    • Python基础知识
    • Python CookBook第三版
    • Flask
  • MySQL

    • MySQL基础知识
    • MySQL调优
    • MySQL面试题
算法
  • FineReport
  • Kettle
  • Git
  • 微信公众号文章
  • 优秀博客文章
  • 其他
收藏夹
  • 分类
  • 标签
  • 归档
GitHub (opens new window)
  • 《Flask》

  • 《Python Cookbook》第三版

    • 第一章:数据结构与算法

    • 第二章:字符串和文本

      • 使用多个界定符分割字符串
      • 字符串开头或结尾匹配
      • 用 Shell 通配符匹配字符串
      • 字符串匹配和搜索
      • 字符串搜索和替换
      • 字符串忽略大小写的搜索替换
      • 最短匹配模式
      • 多行匹配模式
      • 将 Unicode 文本标准化
      • 在正则式中使用 Unicode
      • 删除字符串中不需要的字符
      • 审查清理文本字符串
      • 字符串对齐
      • 合并拼接字符串
      • 字符串中插入变量
      • 以指定列宽格式化字符串
        • 在字符串中处理html和xml
        • 字符串令牌解析
        • 实现一个简单的递归下降分析器
        • 字节字符串上的字符串操作
      • 第三章:数字日期和时间

      • 第四章:迭代器与生成器

      • 第五章:文件与IO

      • 第六章:数据编码和处理

      • 第七章:函数

      • 第八章:类与对象

      • 第九章:元编程

      • 第十章:模块与包

      • 第十一章:网络与Web编程

      • 第十二章:并发编程

      • 第十三章:脚本编程与系统管理

      • 第十四章:测试、调试和异常

      • 第十五章:C语言扩展

    • Python基础

    • Python
    • 《Python Cookbook》第三版
    • 第二章:字符串和文本
    weibw
    2022-01-05

    以指定列宽格式化字符串

    # 问题

    你有一些长字符串,想以指定的列宽将它们重新格式化。

    # 解决方案

    使用 textwrap 模块来格式化字符串的输出。比如,假如你有下列的长字符串:

    s = "Look into my eyes, look into my eyes, the eyes, the eyes, \
    the eyes, not around the eyes, don't look around the eyes, \
    look into my eyes, you're under."
    
    1
    2
    3

    下面演示使用 textwrap 格式化字符串的多种方式:

    >>> import textwrap
    >>> print(textwrap.fill(s, 70))
    Look into my eyes, look into my eyes, the eyes, the eyes, the eyes,
    not around the eyes, don't look around the eyes, look into my eyes,
    you're under.
    >>> print(textwrap.fill(s, 40))
    Look into my eyes, look into my eyes,
    the eyes, the eyes, the eyes, not around
    the eyes, don't look around the eyes,
    look into my eyes, you're under.
    >>> print(textwrap.fill(s, 40, initial_indent=' '))
    	Look into my eyes, look into my
    eyes, the eyes, the eyes, the eyes, not
    around the eyes, don't look around the
    eyes, look into my eyes, you're under.
    >>> print(textwrap.fill(s, 40, subsequent_indent=' '))
    Look into my eyes, look into my eyes,
    the eyes, the eyes, the eyes, not
    around the eyes, don't look around
    the eyes, look into my eyes, you're
    under.
    
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21

    # 讨论

    textwrap 模块对于字符串打印是非常有用的,特别是当你希望输出自动匹配终端大小的时候。你可以使用 os.get_terminal_size() 方法来获取终端的大小尺寸。比如:

    >>> import os
    >>> os.get_terminal_size().columns
    80
    >>>
    
    1
    2
    3
    4

    fill() 方 法 接 受 一 些 其 他 可 选 参 数 来 控 制 tab, 语 句 结 尾 等。参 阅 tex-twrap.TextWrapper (opens new window) 文档 获取更多内容。

    编辑 (opens new window)
    上次更新: 2023/10/13, 17:39:25
    字符串中插入变量
    在字符串中处理html和xml

    ← 字符串中插入变量 在字符串中处理html和xml→

    最近更新
    01
    牛客网非技术快速入门SQL练习题
    03-08
    02
    其他日常SQL题
    03-07
    03
    用户与权限管理
    03-05
    更多文章>
    Theme by Vdoing | Copyright © 2021-2023 | Weibw | 辽ICP备18015889号
    • 跟随系统
    • 浅色模式
    • 深色模式
    • 阅读模式