GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • 首页
  • HTML
  • JavaScript
  • CSS
  • React
  • Python3
  • Java
  • C
  • C++
  • C#
  • Go
  • SQL
  • Linux
  • VS Code

This view

  • Original page
  • New address

GeezerVison is showing www.runoob.com — Python3 expandtabs()方法 | 菜鸟教程

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

  • Vue3 教程
  • Vue2 教程
  • Bootstrap3
  • Bootstrap4
  • Bootstrap5
  • 机器学习
  • PyTorch
  • TensorFlow
  • Sklearn
  • NLP
  • AI Agent
  • Ollama
  • Coding Plan
Python 3 教程
Python3 教程 Python3 简介 Python3 环境搭建 Python3 Pycharm 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
Python3 endswith()方法
Python3 find()方法

Python3 expandtabs()方法

Python3 字符串 Python3 字符串


描述

expandtabs() 方法把字符串中的 tab 符号 \t 转为空格,tab 符号 \t 默认的空格数是 8,在第 0、8、16...等处给出制表符位置,如果当前位置到开始位置或上一个制表符位置的字符数不足 8 的倍数则以空格代替。

语法

expandtabs() 方法语法:

str.expandtabs(tabsize=8)

参数

  • tabsize -- 指定转换字符串中的 tab 符号 \t 转为空格的字符数。

返回值

该方法返回字符串中的 tab 符号 \t 转为空格后生成的新字符串。

实例

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

实例

#!/usr/bin/python3 str = "runoob\t12345\tabc" print('原始字符串:', str) # 默认 8 个空格 # runnob 有 6 个字符,后面的 \t 填充 2 个空格 # 12345 有 5 个字符,后面的 \t 填充 3 个空格 print('替换 \\t 符号:', str.expandtabs()) # 2 个空格 # runnob 有 6 个字符,刚好是 2 的 3 倍,后面的 \t 填充 2 个空格 # 12345 有 5 个字符,不是 2 的倍数,后面的 \t 填充 1 个空格 print('使用 2 个空格替换 \\t 符号:', str.expandtabs(2)) # 3 个空格 print('使用 3 个空格:', str.expandtabs(3)) # 4 个空格 print('使用 4 个空格:', str.expandtabs(4)) # 5 个空格 print('使用 5 个空格:', str.expandtabs(5)) # 6 个空格 print('使用 6 个空格:', str.expandtabs(6))

以上实例输出结果如下:

原始字符串: runoob      12345   abc
替换 \t 符号: runoob  12345   abc
使用 2 个空格替换 \t 符号: runoob  12345 abc
使用 3 个空格: runoob   12345 abc
使用 4 个空格: runoob  12345   abc
使用 5 个空格: runoob    12345     abc
使用 6 个空格: runoob      12345 abc

Python3 字符串 Python3 字符串

AI 思考中...
Python3 endswith()方法
Python3 find()方法

5 篇笔记 写笔记

  1. #0

       yao_yaofu

      522***[email protected]

    26

    \t 解释补充

    \t 是补全当前字符串长度到8的整数倍,最少 1 个最多 8 个空格。

    补多少要看你 \t 前字符串长度。

    比如当前字符串长度 10,那么 \t 后长度是 16,也就是补 6 个空格。

    如果当前字符串长度 12,此时 \t 后长度是 16,补 4 个空格。

    实例:

    str1 = "this is\tstring example....wow!!!"
    str2 = "athis is\tstring example....wow!!!"
    str3 = "athis is        string example....wow!!!"  # is 和 string 中间输入 8 个空格
    print(str1)
    print("a"+str1)
    print(str2)
    print(str3)

    运行结果如下:

    this is string example....wow!!!            # \t 前有 7 个字符,补充 0 个空格
    athis is        string example....wow!!!    # \t 前有 8 个字符,补充 8 个空格
    athis is        string example....wow!!!    # 同上
    athis is        string example....wow!!!    # 8 个空格的效果,做对比
    yao_yaofu

       yao_yaofu

      522***[email protected]

    9年前 (2018-03-05)
  2. #0

       钅钅钅

      153***[email protected]

    16

    在 Python3 中是补 4 的整数倍个空格:

    str1 = "this is\tstring example....wow!!!"
    str2 = "athis iss\tstring example....wow!!!"
    str3 = "athis i\tstring example....wow!!!"
    str4 = "athis is    string example....wow!!!"  # is 和 string 中间输入 8 个空格
    print(str1)
    print("a"+str1)
    print(str2)
    print(str3)
    print(str4)

    输出结果为:

    this is    string example....wow!!!
    athis is    string example....wow!!!
    athis iss    string example....wow!!!
    athis i    string example....wow!!!
    athis is    string example....wow!!!
    
    钅钅钅

       钅钅钅

      153***[email protected]

    9年前 (2018-04-13)
  3. #0

       Rocky2188

      271***[email protected]

    5

    \t:格式化制表符后首字符位置为最小整数倍制表位位置。

    str = "this is\tstring example....wow!!!"
    
    # 删除\t:设置为≤0整数
    print("替换 \\t 符号: " + str.expandtabs(0))
    
    # 当前置字符串长度为整数倍制表位长度时,替换为制表位长度(1空格)
    print("替换 \\t 符号: " + str.expandtabs(1))
    
    # \t被替换为1空格:math.ceil(len("this is")/2)*2-len("this is")
    print("替换 \\t 符号: " + str.expandtabs(2))
    
    # \t被替换为2空格:math.ceil(len("this is")/3)*3-len("this is")
    print("替换 \\t 符号: " + str.expandtabs(3))
    
    # 当前置字符串长度为整数倍制表位长度时,替换为制表位长度(7空格)
    print("替换 \\t 符号: " + str.expandtabs(7))

    输出:

    替换 \t 符号: this isstring example....wow!!!
    替换 \t 符号: this is string example....wow!!!
    替换 \t 符号: this is string example....wow!!!
    替换 \t 符号: this is  string example....wow!!!
    替换 \t 符号: this is       string example....wow!!!
    Rocky2188

       Rocky2188

      271***[email protected]

    6年前 (2020-07-31)
  4. #0

       一条猫

      m-y***[email protected]

    8

    关于 \t 空格,自己写了写发现并非补全 4 或 8 的整数倍:

    str1='1\t11'
    str2='1234567\t11'              
    str3='123456789\t11'
    str4='1234567890\t11'
    str5='123456789012\t11'
    str6='123456789012345678\t11'
    print(str1)
    print(str2)
    print(str3)
    print(str4)
    print(str5)
    print(str6)

    输出结果为:

    1 11    #\t前有一个字符,补1空格
    1234567   11    #\t前有(1,10)个字符,只要在范围内,都补全10个字符(不包括\t后字符)                 
    123456789 11
    1234567890        11    #\t前有10字符,补8空格    
    123456789012        11    #\t前有(10,18)字符,都补全18字符(不包括\t后字符)           
    123456789012345678        11    #\t前满18补8(以此类推) 

    综上,发现 \t补全的空格数 加 \t之前的字符数 呈现规律为等差数列:2,2+8,2+8+8,······

    一条猫

       一条猫

      m-y***[email protected]

    6年前 (2020-08-14)
  5. #0

       Cola

      124***[email protected]

    7

    2022年更新一下制表符的东西,对于楼上的进行修正,横向制表符输出空格的时候统计个数包括了该行的所有字符,文字(占两个字符)符号等等

    版本:python3.11

    str1='1\t11'
    str2='1234567\t11'  
    str3='123456789\t11'
    str4='1234567890\t11'
    str5='123456789012\t11'
    str6='123456789012345678\t11'
    
    print(str1)
    print(str2)
    print(str3)
    print(str4)
    print(str5)
    print(str6)

    输出结果:

    1       11    #少于八位补齐八位
    1234567 11
    123456789       11 #大于八位补齐八的倍数且最接近的倍数
    1234567890      11
    123456789012    11
    123456789012345678      11
    Cola

       Cola

      124***[email protected]

    4年前 (2022-11-20)

点我分享笔记

取消

  • 昵称 (必填)
  • 邮箱 (必填)
  • 引用地址
分类导航
  • Python / 数据科学
    • Python 教程
    • Python2.x 教程
    • FastAPI 教程
    • Flask 教程
    • Django 教程
    • NumPy 教程
    • Pandas 教程
    • SciPy 教程
    • Matplotlib 教程
    • Dash 教程
    • Jupyter Notebook 教程
    • Pillow 教程
    • 量化交易
    • R 教程
    • Julia 教程
  • AI / 智能开发
    • AI Agent(智能体)
    • AI(人工智能)
    • AI 应用案例
    • Codex 教程
    • Vibe Coding 教程
    • Claude Code
    • OpenCode
    • Skills(技能)
    • Ollama 教程
    • Hermes Agent
    • DeepSeek Harness
    • Pi Agent
    • AI 数学基础
    • TensorFlow 教程
    • PyTorch 教程
    • Scikit-learn 教程
    • 机器 教程
    • LangChain
    • 自然语言处理 NLP
    • OpenCV 教程
    • Selenium 教程
    • Playwright 教程
  • 前端开发
    • HTML 教程
    • HTML5 教程
    • CSS 教程
    • CSS3 教程
    • JavaScript 教程
    • HTML DOM 教程
    • TypeScript 教程
    • AJAX 教程
    • JSON 教程
    • Tailwind CSS 教程
    • Bootstrap4 教程
    • Bootstrap5 教程
    • Foundation 教程
    • Vue.js 教程
    • Vue3 教程
    • React 教程
    • Next.js 教程
    • Three.js 教程
    • AngularJS 教程
    • Angular 教程
    • jQuery 教程
    • jQuery UI 教程
    • jQuery EasyUI 教程
    • ECharts 教程
    • Chart.js 教程
    • Highcharts 教程
    • Google 地图 教程
    • SVG 教程
    • Font Awesome 教程
  • 后端开发
    • Node.js 教程
    • Electron 教程
    • PHP 教程
    • Java 教程
    • Go 教程
    • Rust 教程
    • C# 教程
    • Unity 教程
    • Servlet 教程
    • JSP 教程
    • ASP 教程
    • AppML 教程
    • VBScript 教程
    • Swagger 教程
    • RESTful API 教程
    • Docker 教程
    • Linux 教程
    • ZooKeeper 教程
  • 数据库
    • SQL 教程
    • MySQL 教程
    • PostgreSQL 教程
    • SQLite 教程
    • MongoDB 教程
    • Redis 教程
    • Memcached 教程
  • 移动开发
    • Android 教程
    • Flutter 教程
    • Ionic 教程
    • jQuery Mobile 教程
    • Swift 教程
    • Kotlin 教程
  • DevOps / 工程化
    • Git 教程
    • SVN 教程
    • CMake 教程
    • Maven 教程
    • VS Code 教程
    • Obsidian 教程
    • PyCharm 教程
    • Eclipse 教程
    • Markdown 教程
    • LaTeX 教程
  • 编程语言
    • C 教程
    • C++ 教程
    • Zig 教程
    • Scala 教程
    • Ruby 教程
    • Perl 教程
    • Lua 教程
    • Dart 教程
    • 汇编语言 教程
    • Verilog 教程
  • 计算机基础
    • 计算机组成原理
    • 数据结构与算法
    • C 语言数据结构与算法
    • 设计模式
    • Python 设计模式
    • 正则表达式
    • HTTP 教程
    • TCP/IP 教程
    • 网络协议
    • W3C 教程
  • XML / Web Service
    • XML 教程
    • DTD 教程
    • XML DOM 教程
    • XSLT 教程
    • XPath 教程
    • XQuery 教程
    • XLink 教程
    • XPointer 教程
    • XML Schema 教程
    • XSL-FO 教程
    • Web Service 教程
    • WSDL 教程
    • SOAP 教程
    • RSS 教程
    • RDF 教程
  • .NET
    • ASP.NET 教程
    • MVC 教程
    • Razor 教程
    • Web Forms 教程
    • Web Pages 教程
    • PowerShell 教程
  • 网站建设
    • 网站建设指南
    • 浏览器信息
    • 网站主机教程
    • 网站品质

字节 Coding Plan 限时 9.9 元起,支持 Deepseek-V4、GLM-5.3、Doubao-Seed-Evolving、Kimi-K3。
百度 Dumate 百度推出的桌面级 AI 智能体。 输入激活码送 4000 积分
使用文档
Trae AI 原生开发环境,让编程更高效。新用户注册赠2500积分,管理项目,也交付成果。
Advertisement
You are visitor 01402775 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.