python之字符串常见操作,包括字符串的每个单词的首字母大写、所有大写字母小写、所有大写字母小写、字符串左对齐、右对齐、居中等等
1、如果设置字符串name="how are you and how do you do!"title ===>格式:name.title()说明:把字符串的每个单词首字母大写>>> name="how are you and how do you do!">>> name.title()'How Are You And How Do You Do!'>>> name.title(0,10)Traceback (most recent call last): File "<stdin>", line 1, in <module>TypeError: title() takes no arguments (2 given)
2、startswith ===>格式:name.startswith(obj)说明:检查字符串是否是以 obj 开头, 是则返回 True,否则返回 False>>> name.startswith("how")True>>> name.startswith("are")False>>> name.startswith("ho")
3、endswith ===>格式:name.endswith(obj)说明:检查字符串是否以obj结束,如果是返回True,否则返回 False.>>> name.endswith("how")False>>> name.endswith("do")False>>> name.endswith("!")
4、lower ===》格式:name.lower()说明:转换 name中所有大写字符为小写>>> user="How Are you and HOW">>> user.lower()'how are you and how'upper ===》格式:name.uooer()说明:转换 name中小写字符为大写>>> name.upper()'HOW ARE YOU AND HOW DO YOU DO!'
5、ljust ===>格式:name.ljust(width)说明:返回一个原字符串左对齐,并使用空格填充至长度 width 的新字符串rjust ===>格式:name.rjust(width)说明:返回一个原字符串右对齐,并使用空格填充至长度 width 的新字符串
6、center ===>格式:name.center(width)说明:返回一个原字符串居中,并使用空格填充至长度 width 的新字符串>>> temp="hello ">>> temp.center(2)'hello '
更多关于python自动化测试学习资料可加博主qq:1993712276,或者去测码官网查看