字符串大小写转换
【1】capitalize():将字符串第一个字符转换为大写
mystr='hello word and itcast and itheima and python'
print(mystr.capitalize())
# Hello word and itcast and itheima and python
【2】title():将字符串每个单词首字母转换为大写
mystr='hello word and itcast and itheima and python'
print(mystr.title())
# Hello Word And Itcast And Itheima And Python
【3】lower():在字符串中大写变小写
mystr='Hello Word And Itcast And Itheima And Python'
print(mystr.lower())
# hello word and itcast and itheima and python
【4】upper ():将字符串小写变大写
mystr='hello word and itcast and itheima and python'
print(mystr.upper())
# HELLO WORD AND ITCAST AND ITHEIMA AND PYTHON
删除
【1】:lstrip():删除字符串左侧空白字符
mystr=' hello word and itcast and itheima and python'
print(mystr.lstrip())
# 输出:hello word and itcast and itheima and python
【2】rstrip():删除最右边的空格
mystr=' hello word and itcast and itheima and python '
print(mystr.rstrip())
# 输出: hello word and itcast and itheima and python**
【3】strip():删除两侧的空格
mystr=' hello word and itcast and itheima and python '
print(mystr.strip())
# 输出:hello word and itcast and itheima and python
左中右对齐
【1】ljust():返回一个原字符串左对齐,并使用指定字符(默认空格)填充至对应长度的新字符串
1.语法:
字符串序列.ljust(长度,填充字符)
【2】rjust():右对齐
【3】center():居中对齐,与just一致
实例:
判断真假
返回布尔型的数据类型:True和False
【1】stratswith():检查字符串是否是以指定子串开头,是则返回True,否则返回False。如果设置开始和结束位置下标,则在指定位置内检查
1.语法:
字符串序列.stratswith(子串,开始下标,结尾下标)
体验:
mystr='hello word and itcast and itheima and python '
print(mystr.startswith('hello'))
# True
print(mystr.startswith('hellos'))
# False
【2】endswith():看结尾是不是某个字符串
用法类似stratswith
mystr='hello word and itcast and itheima and python'
print(mystr.endswith('python'))
# True
print(mystr.endswith('hellos'))
# False
【3】isalpha():如果字符串至少一个字符并且所有字符都是字母则返回True,否则False
mystr='hello word and itcast and itheima and python'
print(mystr.isalpha())
# False
mystr='hellowordanditcastanditheimaandpython'
print(mystr.isalpha())
# True
【4】isdigit():如果字符串只包括数字则True否则False
mystr='hellowordanditcastanditheimaandpython '
print(mystr.isdigit())
# False
mystr='11122222'
print(mystr.isdigit())
# True
【5】isalnum():如果字符串至少有一个字符并且所有字符都是数字或者字母则True否则False
mystr='hellowordanditcastanditheimaandpython '
print(mystr.isalnum())
# False
mystr='hello1word2and3itcast4and5itheima6and7python'
print(mystr.isalnum())
# True
【6】isspace():如果字符串中只包括空白,则True否则False
mystr='hellowordanditcastanditheimaandpython '
print(mystr.isspace())
# False
mystr1=' '
print(mystr1.isspace())
# True
字符串学完了,我脑袋也忘记完了