一.字典操作 keys items values
代码段
dica = {"chenzhang":25,"joy":28,"hebe":33}
print("取字典的键:",sorted(dica.keys()))
print("取字典的键值对:",sorted(dica.items()))
print("取字典的值:",sorted(dica.values()))
输出结果
取字典的键: ['chenzhang', 'hebe', 'joy']
取字典的键值对: [('chenzhang', 25), ('hebe', 33), ('joy', 28)]
取字典的值: [25, 28, 33]
循环遍历
代码段
dica = {"chenzhang":25,"joy":28,"hebe":33}
for i in dica:
print("循环遍历键:",i)
print("\n") # 打印空行
for i,v in dica.items():
print("循环遍历键值对:",i,v)
输出结果
循环遍历键: chenzhang
循环遍历键: joy
循环遍历键: hebe
循环遍历键值对: chenzhang 25
循环遍历键值对: joy 28
循环遍历键值对: hebe 33
二.String 操作
代码段
stra = "joy....ll...."
print("重复输入10个字符串:",stra*10)
print("通过切片的方式获取字符串:","helloworld"[2:6])
输出结果
重复输入10个字符串: joy....ll....joy....ll....joy....ll....joy....ll....joy....ll....joy....ll....joy....ll....joy....ll....joy....ll....joy....ll....
通过切片的方式获取字符串: llow
三.关键字 in
代码段
print("查看列表中是否包含123:",123 in [23,45,123])
x = "hello"
print("e2是否包含在hello里面:",'e2' in x)
输出结果
查看列表中是否包含123: True
e2是否包含在hello里面: False
四.字符串方法 格式化输出、字符串拼接 join
代码段
print("格式化输出:",'%s is a good teacher'%'joy')
a = '123'
b = 'abc'
c = a+b
print("a+b字符串拼接:",c)
d = '44'
c = ''.join([a,b,d])
print("join方法拼接:",c)
输出结果
格式化输出: joy is a good teacher
a+b字符串拼接: 123abc
join方法拼接: 123abc44
五.字符串方法 查、改、增、判断
1.查 count find index rfind
代码段
print("统计元素出现个数:",st.count('l'))
print("查找到第一个元素,并将索引值返回:",st.find('t'))
print("查找到第一个元素的索引值:",st.index('t'))
print("从右到左,打印字符最后一次出现的位置:",'My title title'.rfind('t'))
输出结果
统计元素出现个数: 2
查找到第一个元素,并将索引值返回: 8
查找到第一个元素的索引值: 8
从右到左,打印字符最后一次出现的位置: 11
2.改 capitalize format format_map lower upper swapcase strip lstrip rstrip replace split title
代码段
st='hello kitty {name} is {age}'
print("修改首字母大写:",st.capitalize())
print("格式化输出的另一种方式:",st.format(name='joy',age=28))
print("使用字典的方式格式化输出:",st.format_map({'name':'hebe','age':34}))
print("将大写字母变为小写:",'My tLtle'.lower())
print("将小写字母变为大写:",'My tLtle'.upper())
print("将大小写字母进行转换:",'My tLtle'.swapcase())
print("用于移除字符串头尾指定的字符(默认为空格或换行符)或字符序列:",'\tMy tLtle\n'.strip())
print("用于截掉字符串左边的空格或指定字符:",'\tMy tLtle\n'.lstrip())
print("用于截掉字符串右边的空格或指定字符:",'\tMy tLtle\n'.rstrip())
print("将原有字符替换成新字符,后面加替换个数:",'My title title'.replace('itle','lesson',1))
print("以i为分割符,分割1次:",'My title title'.split('i',1))
print("将字符串,转换成标题格式(即每个单词的首字母为大写):",'My title title'.title())
结果输出
修改首字母大写: Hello kitty {name} is {age}
格式化输出的另一种方式: hello kitty joy is 28
使用字典的方式格式化输出: hello kitty hebe is 34
将大写字母变为小写: my tltle
将小写字母变为大写: MY TLTLE
将大小写字母进行转换: mY TlTLE
用于移除字符串头尾指定的字符(默认为空格或换行符)或字符序列: My tLtle
用于截掉字符串左边的空格或指定字符: My tLtle
用于截掉字符串右边的空格或指定字符: My tLtle
将原有字符替换成新字符,后面加替换个数: My tlesson title
以i为分割符,分割1次: ['My t', 'tle title']
将字符串,转换成标题格式(即每个单词的首字母为大写): My Title Title
3.增 center expandtabs ljust rjust
代码段
print("打印包含#的50个字符串,居中格式化输出:",st.center(50,'#'))
sta = 'a(\t)'
print("符号t默认的空格数是 10:",sta.expandtabs(tabsize=10))
print("原字符串左对齐,打印50个字符,多余的用*填充:",'My tLtle'.ljust(50,'*'))
print("原字符串右对齐,打印50个字符,多余的用*填充:",'My tLtle'.rjust(50,'*'))
输出结果
打印包含#的50个字符串,居中格式化输出: ###########hello kitty {name} is {age}############
符号t默认的空格数是 10: a( )
原字符串左对齐,打印50个字符,多余的用*填充: My tLtle******************************************
原字符串右对齐,打印50个字符,多余的用*填充: ******************************************My tLtle
4.判断 布尔值 endswith startswith isalnum isdecimal isnumeric isidentifier islower isupper isspace istitle
代码段
st='hello kitty {name} is {age}'
print("判断是否以某个内容结尾,返回值True/False:",st.endswith('e}'))
print("判断是否以某个内容开头,返回值True/False:",st.startswith('he'))
print("判断字符中是否存在字母和数字:",'as1'.isalnum())
print("判断字符中是否存在数字:",'12632178'.isdecimal())
print("判断字符串中是否所有的都是数字:",'1269999aa'.isnumeric())
print("可用来判断变量名是否合法:",'abc'.isidentifier())
print("判断是否为全部小写字母:",'Aab'.islower())
print("判断是否为全部大写字母:",'ABC'.isupper())
print("判断是否为全部空格字符:",' '.isspace())
print("判断是否所有单词首字母为大写:",'My title'.istitle())
输出结果
判断是否以某个内容结尾,返回值True/False: True
判断是否以某个内容开头,返回值True/False: True
判断字符中是否存在字母和数字: True
判断字符中是否存在数字: True
判断字符串中是否所有的都是数字: False
可用来判断变量名是否合法: True
判断是否为全部小写字母: False
判断是否为全部大写字母: True
判断是否为全部空格字符: True
判断是否所有单词首字母为大写: False
六.常用方法
代码段
st='hello kitty {name} is {age}'
print(st.count('l'))
print(st.center(50,'#')) # 居中
print(st.startswith('he')) # 判断是否以某个内容开头
print(st.find('t'))
print(st.format(name='alex',age=37)) # 格式化输出的另一种方式 待定:?:{}
print('My tLtle'.lower())
print('My tLtle'.upper())
print('\tMy tLtle\n'.strip())
print('My title title'.replace('itle','lesson',1))
print('My title title'.split('i',1))
输出结果
2
###########hello kitty {name} is {age}############
True
8
hello kitty alex is 37
my tltle
MY TLTLE
My tLtle
My tlesson title
['My t', 'tle title']