• 技术文章 >Python技术 >Python基础教程

    Python expandtabs()方法

    silencementsilencement2019-07-04 13:31:45原创2282

    描述

    Python expandtabs() 方法把字符串中的 tab 符号('\t')转为空格,默认的空格数 tabsize 是 8。

    语法

    expandtabs()方法语法:

    1

    string.expandtabs(tabsize=8)

    参数

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

    返回值

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

    实例

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

    1

    2

    3

    4

    5

    6

    7

    #!/usr/bin/python

      

    string = "this is\tstring example....wow!!!";

      

    print "Original string: " + string

    print "Defualt exapanded tab: " +  string.expandtabs()

    print "Double exapanded tab: " +  string.expandtabs(16)

    以上实例输出结果如下

    1

    2

    3

    Original string: this is        string example....wow!!!

    Defualt exapanded tab: this is string example....wow!!!

    Double exapanded tab: this is         string example....wow!!!

    专题推荐:expandtabs方法
    上一篇:Python类型转换,数据类型转换函数大全 下一篇:Python endswith()方法

    相关文章推荐

    • Python input()函数:获取用户输入的字符串

    全部评论我要评论

    © 2021 Python学习网 苏ICP备2021003149号-1

  • 取消发布评论
  • 

    Python学习网