Python string expandtabs() function returns a new string with tab characters (\t) replaced with one or more whitespaces.

Python字符串expandtabs()函数返回一个新的字符串,其中的制表符(\ t)替换为一个或多个空格。

Python字符串expandtabs() (Python String expandtabs())

This function syntax is:

该函数语法为:

str.expandtabs(tabsize=8)

We can specify the tabsize parameter to specify the number of whitespaces to use when expanding the tab characters. The default value of the tabsize parameter is 8.

我们可以指定tabsize参数来指定扩展制表符时要使用的空格数。 tabsize参数的默认值为8。

Python takes into account the characters present before the tab character is found. The number of whitespaces to use is equal to the tabsize minus the number of characters before the tab character.

Python将在找到制表符之前考虑存在的字符。 要使用的空格数等于制表符大小减去制表符前的字符数。

If the number of characters is more than the tabsize then the next multiple of tabsize is used i.e. 8,16,24 and so on.

如果字符数大于制表符的大小,则使用制表符的下一个倍数,即8,16,24,依此类推。

If there are multiple tab characters to be replaced, then the characters before the tab are counted only until the previous tab character is reached.

如果要替换多个制表符,则仅计算制表符之前的字符,直到到达前一个制表符为止。

Python字符串expandtabs()示例 (Python String expandtabs() example)

Let’s look at a simple example of expandtabs() function.

让我们来看一个expandtabs()函数的简单示例。

s = 'A\tB\tC\tD'print(s.expandtabs())# Output:
# A       B       C       D

Notice that there is exactly one character before every tab character, so 7 whitespaces are used to replace tab character.

请注意,每个制表符之前只有一个字符,因此使用7个空格替换制表符。

Let’s look at another example with more characters before tab character.

让我们看另一个在制表符之前有更多字符的示例。

s = 'ABCD\tE\tF'print(s.expandtabs())# Output:
# ABCD    E       F

There are 4 characters before the first tab character, so 8-4=4 whitespaces are used to expand the tab character.

第一个制表符前面有4个字符,因此使用8-4 = 4空格扩展制表符。

For the second tab character, there is only 1 character before it, so 8-1=7 whitespaces are used to expand the tab character.

对于第二个制表符,前面只有1个字符,因此使用8-1 = 7空格来扩展制表符。

Let’s look at another example where the number of characters before tab character is more than the tabsize.

让我们看另一个示例,其中制表符之前的字符数大于制表符大小。

s = 'ABCDEFGHIJK\tG'
print(s.expandtabs())# Output:
# ABCDEFGHIJK     G

There are 11 characters before the tab, so 8*2-11=5 whitespaces are used to expand the tab.

选项卡前有11个字符,因此使用8 * 2-11 = 5空格来扩展选项卡。

Let’s look at another example of this scenario.

让我们看一下这种情况的另一个例子。

s = 'ABCDEFGHIJK\t\tG'
print(s.expandtabs())# Output:
# ABCDEFGHIJK             G

The first tab is expanded with 8*2-11=5 whitespaces whereas the second tab is expanded with 8-0=8 whitespaces. That’s why there are 13 whitespaces in the output string.

第一个选项卡扩展为8 * 2-11 = 5个空格,而第二个选项卡扩展为8-0 = 8个空格。 这就是为什么输出字符串中有13个空格的原因。

带有tabsize的Python字符串expandtabs() (Python string expandtabs() with tabsize)

Let’s look at some examples with variable tabsize.

让我们看一些带有变量tabsize的示例。

s = 'ABC\tD'
print(s)
print(s.expandtabs())
print(s.expandtabs(tabsize=0))
print(s.expandtabs(tabsize=1))
print(s.expandtabs(tabsize=2))
print(s.expandtabs(tabsize=3))
print(s.expandtabs(tabsize=4))
print(s.expandtabs(tabsize=5))
print(s.expandtabs(tabsize=6))
print(s.expandtabs(tabsize=7))

Just follow the rules above and you will be able to easily understand the output.

只要遵循上述规则,您就可以轻松理解输出。

Can we specify a negative value for tabsize? Let’s check it out with a simple code snippet.

我们可以为tabsize指定一个负值吗? 让我们用一个简单的代码片段进行检查。

s = 'ABC\tD'
print(s.expandtabs(tabsize=-1))
print(s.expandtabs(tabsize=-3))

Output:

输出:

ABCD
ABCD

It’s clear that Python is using tabsize as 0 when a negative tabsize value is provided and doesn’t throw any error.

显然,当提供负的tabsize值且不会引发任何错误时,Python会将tabsize用作0。

GitHub Repository.GitHub存储库中检出Python脚本和更多字符串示例。

Reference: Official Documentation

参考: 官方文档

翻译自: https://www.journaldev.com/23978/python-string-expandtabs

Python字符串expandtabs()相关推荐

  1. python 字符串函数_Python字符串函数

    python 字符串函数 Python provides a lot of built-in functions to manipulate strings. Python String is imm ...

  2. 真香!精心整理了 100+Python 字符串常用操作

    来源丨萝卜大杂烩 作者丨周萝卜 字符串作为平时使用最多的数据类型,其常用的操作我们还是很有必要熟记于心的,本文整理了多种字符串的操作的案例,还是非常用心,记得点赞收藏~ 字符串切片操作 test = ...

  3. Python字符串基本操作

    Python字符串基本操作 1.判断是不是合法的标识符isidentifier name="ABC" print(name.isidentifier()) 打印结果 True 2. ...

  4. Python 字符串方法详解

    Python 字符串方法详解 本文最初发表于赖勇浩(恋花蝶)的博客(http://blog.csdn.net/lanphaday),如蒙转载,敬请保留全文完整,切勿去除本声明和作者信息. 在编程中,几 ...

  5. 2.1.Python字符串处理(去掉空格或者特殊字符、替换操作、查找操作、判断操作、分割合并操作、字符串文档)

    2.1.Python字符串处理 2.1.1.去掉空格或者特殊字符 2.1.2.替换操作 2.1.3.查找操作 2.1.4.判断操作 2.1.5.分割合并操作 2.1.6.字符串文档 2.1.Pytho ...

  6. 7.python字符串-内置方法分析

    上篇对python中的字符串内置方法进行了列举和简单说明,但这些方法太多,逐一背下效率实在太低,下面我来对这些方法按照其功能进行总结: 1.字母大小写相关(中文无效) 1.1 S.upper() -& ...

  7. Python字符串笔录

    python字符串操作实方法,包括了几乎所有常用的python字符串操作,如字符串的替换.删除.截取.复制.连接.比较.查找.分割等 1.去空格及特殊符号 >>> s = '123 ...

  8. python字符串函数运算_Python入门教程2. 字符串基本操作【运算、格式化输出、常用函数】 原创...

    前面简单介绍了python基本运算,这里再来简单讲述一下Python字符串相关操作 1. 字符串表示方法 >>> "www.jb51.net" #字符串使用单引号 ...

  9. python字符串的表示_Python字符串方法总结

    Python字符串方法图示: (温馨提示:对图片点右键--在新标签页中打开图片) 1.index() 定义:查找并返回指定str的索引位置,如果没找到则会抛异常(查找的顺序是从左至右)可以指定范围:开 ...

最新文章

  1. c语言程序既可以编译执行也可以解释执行,2016年山东农业大学信息科学与工程学院C语言程序设计(同等学力加试)复试笔试仿真模拟题...
  2. 利用DHCP,http,tftp,pxe实现批量自动化部署系统
  3. Boost:额外的bimap的测试程序
  4. 直方图绘制与直方图均衡化实现
  5. docker 中用docker 启动应用访问docker中的mysql
  6. jq监听input type=file发生改变,即选择文件,并获取文件名称
  7. go-echarts x 轴标签显示不全
  8. VueCli4学习笔记
  9. 装机之必备软件下载合集
  10. 科研常用到的计算机编程,科研必备:几款好用的流程图工具,助力你的论文/科研绘图...
  11. SublimeText3 搭建 C++ Python
  12. Java实现对文件的读写操作
  13. POI 导出Excel
  14. MySQL之mysqlcheck、check、optimize和analyze
  15. java容器都有哪些
  16. 李沐动手学深度学习笔记---含并行连结的网络 GoogLeNet / Inception V3
  17. maven远程仓库和镜像
  18. Excel催化剂开源第10波-VSTO开发之用户配置数据与工作薄文件一同存储
  19. 【通信原理】实验二 角度调制实验
  20. 双11买的开发板,仪器,学电子的男生被优惠哭了。

热门文章

  1. 小白成长建议(9)-苞丁解牛
  2. 《Python核心编程》第二版第209页第八章练习 -Python核心编程答案-自己做的-
  3. eclipse 下安装插件
  4. 网上Silverlight项目收集
  5. [转载] c++的vector赋值方法汇总
  6. Redis 订阅与发布
  7. 顺利通过EMC实验(13)
  8. 二十六. Python基础(26)--类的内置特殊属性和方法
  9. 进程和线程的定义、区别与联系
  10. javascript中的内存泄漏