Python expandtabs()与endswith()方法

 Python expandtabs()方法

描述

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

语法

expandtabs()方法语法:

string.expandtabs(tabsize=8)

参数

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

返回值

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

实例

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

#!/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)

以上实例输出结果如下

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

 Python endswith()方法

描述

Python endswith() 方法用于判断字符串是否以指定后缀结尾,如果以指定后缀结尾返回True,否则返回False。可选参数"start"与"end"为检索字符串的开始与结束位置。

语法

endswith()方法语法:

string.endswith(suffix[, start[, end]])
# 自Python2.5版本起,还支持接收一个 tuple 为参数
string.endswith(tuple) # 满足tuple任何一个都返回True

参数

suffix -- 该参数可以是一个字符串或者是一个元素This could be a string or could also be a tuple of suffixes to look for.

start -- 字符串中的开始位置。

end -- 字符中结束位置。

返回值

如果字符串含有指定的后缀返回True,否则返回False。

实例

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

#!/usr/bin/python
 
string = "this is string example....wow!!!";
 
suffix = "wow!!!";
print string.endswith(suffix);
print string.endswith(suffix,20);
 
suffix = "is";
print string.endswith(suffix, 2, 4);
print string.endswith(suffix, 2, 6);
 
print '-----------------------------'
 
string1 = 'abc'
string2 = 'xyz'
string3 = 'twz'
for string in [string1,string2,string3]:
    print string.endswith(('a','x'))

以上实例输出结果如下:

True
True
True
False
-----------------------------
True
True
False

相关推荐

  1. Python3 笔记:字符串的 startswith() 和 endswith()

    2024-01-14 00:16:02       26 阅读
  2. unsqueeze() 方法squeeze() 方法

    2024-01-14 00:16:02       28 阅读
  3. 实例方法静态方法

    2024-01-14 00:16:02       34 阅读
  4. 【.NET Core】匿名方法扩展方法

    2024-01-14 00:16:02       62 阅读
  5. 封装数据请求方法接口方法

    2024-01-14 00:16:02       46 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-01-14 00:16:02       94 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-01-14 00:16:02       101 阅读
  3. 在Django里面运行非项目文件

    2024-01-14 00:16:02       82 阅读
  4. Python语言-面向对象

    2024-01-14 00:16:02       91 阅读

热门阅读

  1. Unity-游戏与帧

    2024-01-14 00:16:02       62 阅读
  2. 编程探秘:Python深渊之旅-----算法的舞蹈(二)

    2024-01-14 00:16:02       67 阅读
  3. Uber,Meta随着股票下滑而减少招聘

    2024-01-14 00:16:02       67 阅读
  4. getopt()函数详细解释!保证看明白

    2024-01-14 00:16:02       55 阅读
  5. effective c++ 笔记 条款5-12

    2024-01-14 00:16:02       57 阅读