Python expandstabs()方法用分隔的空格替换所有字符。默认情况下,单个选项卡会扩展为8个空格,可以根据需要覆盖这些空格。
无涯教程可以将1、2、4等传递给该方法,该方法将使用这些空格字符替换tab。
expandtabs - 语法
expandtabs(tabsize=8)
expandtabs - 参数
tabsize :它是可选的,默认值为8。
expandtabs - 返回类型
它返回修改后的字符串。
不指定空格的调用方法。设置为默认值。
# Python endswith() function example # 变量声明 str = "Welcome \t to \t the \t Learnfk." # 函数调用 str2 = str.expandtabs() # 显示结果 print(str2)
输出:
Welcome to the Learnfk.
每种方法都设置为不同数量的空格。
# Python endswith() function example # 变量声明 str = "Welcome \t to \t the \t Learnfk." # 函数调用 str2 = str.expandtabs(1) str3 = str.expandtabs(2) str4 = str.expandtabs(4) # 显示结果 print(str2) print(str3) print(str4)
输出:
Welcome to the Learnfk. Welcome to the Learnfk. Welcome to the Learnfk.