元祖、列表、字典
字符串输出
name = 'itheima'
position = '讲师'
address = '北京市'
print('--------------------------------------------------')
print("姓名:%s" % name)
print("职位:%s" % position)
print("公司地址:%s" % address)
print('--------------------------------------------------')
字符串输入
userName = input('请输入用户名:')
print("用户名为:%s" % userName)
password = input('请输入密码:')
print("密码为:%s" % password)
下标和切片
<1>find
检测 str 是否包含在 mystr中,如果是返回开始的索引值,否则返回-1
mystr = 'hello world itcast and itcastcpp
mystr.find(str, start=0, end=len(mystr))
<2>index
跟find()方法一样,只不过如果str不在 mystr中会报一个异常.
mystr = 'hello world itcast and itcastcpp
mystr.index(str, start=0, end=len(mystr)
<3>count
返回 str在start和end之间 在 mystr里面出现的次数
mystr = 'hello world itcast and itcastcpp
mystr.count(str, start=0, end=len(mystr))
<4>replace
把 mystr 中的 str1 替换成 str2,如果 count 指定,则替换不超过 count 次.
mystr = 'hello world itcast and itcastcpp
mystr.replace(str1, str2, mystr.count(str1))
<5>split
以 str 为分隔符切片 mystr,如果 maxsplit有指定值,则仅分隔 maxsplit 个子字符串
mystr = 'hello world itcast and itcastcpp
mystr.split(str=" ", 2)
<6>capitalize
把字符串的第一个字符大写
mystr = 'hello world itcast and itcastcpp
mystr.capitalize()
<7>title
把字符串的每个单词首字母大写
>>> a = "hello itcast"
>>> a.title()
'Hello Itcast'
<8>startswith
检查字符串是否是以 hello 开头, 是则返回 True,否则返回 False
mystr = 'hello world itcast and itcastcpp
mystr.startswith(hello)
<9>endswith
检查字符串是否以obj结束,如果是返回True,否则返回 False.
mystr = 'hello world itcast and itcastcpp
mystr.endswith(obj)
<10>lower
转换 mystr 中所有大写字符为小写
mystr = 'hello world itcast and itcastcpp
mystr.lower()
<11>upper
转换 mystr 中的小写字母为大写
mystr = 'hello world itcast and itcastcpp
mystr.upper()
<12>ljust
返回一个原字符串左对齐,并使用空格填充至长度 width 的新字符串
mystr = 'hello world itcast and itcastcpp
mystr.ljust(width)
<13>rjust
返回一个原字符串右对齐,并使用空格填充至长度 width 的新字符串
mystr = 'hello world itcast and itcastcpp
mystr.rjust(width)
<14>center
返回一个原字符串居中,并使用空格填充至长度 width 的新字符串
mystr = 'hello world itcast and itcastcpp
mystr.center(width)
<15>lstrip
删除 mystr 左边的空白字符
mystr = 'hello world itcast and itcastcpp
mystr.lstrip()
<16>rstrip
删除 mystr 字符串末尾的空白字符
mystr = 'hello world itcast and itcastcpp
mystr.rstrip()
<17>strip
删除mystr字符串两端的空白字符
>>> a = "\n\t itcast \t\n"
>>> a.strip()
<18>rfind
类似于 find()函数,不过是从右边开始查找.
mystr = 'hello world itcast and itcastcpp
mystr.rfind(str, start=0,end=len(mystr)
<19>rindex
类似于 index(),不过是从右边开始.
mystr = 'hello world itcast and itcastcpp
mystr.rindex( str, start=0,end=len(mystr))
<20>partition
把mystr以str分割成三部分,str前,str和str后
mystr = 'hello world itcast and itcastcpp
mystr.partition(str)
<21>rpartition
类似于 partition()函数,不过是从右边开始.
mystr = 'hello world itcast and itcastcpp
mystr.rpartition(str)
<22>splitlines
按照行分隔,返回一个包含各行作为元素的列表
mystr = 'hello world itcast and itcastcpp
mystr.splitlines()
<23>isalpha
如果 mystr 所有字符都是字母 则返回 True,否则返回 False
mystr = 'hello world itcast and itcastcpp
mystr.isalpha()
<24>isdigit
如果 mystr 只包含数字则返回 True 否则返回 False.
mystr = 'hello world itcast and itcastcpp
mystr.isdigit()
#<25>isalnum
如果 mystr 所有字符都是字母或数字则返回 True,否则返回 False
mystr = 'hello world itcast and itcastcpp
mystr.isalnum()
<26>isspace
如果 mystr 中只包含空格,则返回 True,否则返回 False.
mystr = 'hello world itcast and itcastcpp
mystr.isspace()
<27>join
mystr 中每个元素后面插入str,构造出一个新的字符串
mystr = 'hello world itcast and itcastcpp
mystr.join(str)
列表的循环遍历
1. 使用for循环
namesList = ['xiaoWang','xiaoZhang','xiaoHua']
for name in namesList:
print(name)
2. 使用while循环
namesList = ['xiaoWang','xiaoZhang','xiaoHua']
length = len(namesList)
i = 0
while i<length:
print(namesList[i])
i+=1