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

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 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/python3str = "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 思考中...

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

    522***[email protected]

    8年前 (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]

    8年前 (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

    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

    124***[email protected]

    4年前 (2022年11月20日)

点我分享笔记

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

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